[Git] Git整理(三) 远程仓库和远程分支

1.远程仓库

远程仓库是指托管在其他服务器上或网络上的项目版本库,比如Github、GitLab、Gitee等上的仓库。如果要加入一个开源项目,那么就得熟练地掌握远程仓库的使用。这里对远程仓库、远程分支等命令和操作进行下总结。

1.1.获取远程仓库

使用git init会创建一个本地仓库,如果要使用远程仓库,则可以有两种方式可供选择:克隆远程仓库或添加远程仓库。

克隆远程仓库到本地

使用git clone命令将远程仓库克隆到本地:

git clone url

如:

git clone https://github.com/jeraon/pythonLearn.git

当执行git clone命令 时,做了如下几个工作:

  • 1.自动将远程仓库命名为origin,并拉取所有的数据;
  • 2.创建一个指向远程仓库的master分支的指针,并在本地将其命名为origin/master
  • 3.在本地,git会创建一个和origin/master分支指向同一个地方的本地master分支,在本地就可以在该分支工作;

如果想自己命名远程仓库名称,可以使用git clone -o remotename命令,如:

@ubuntu:~/workspace/TestDemo$ git clone -o myorigin https://github.com/jeraon/pythonLearn.git
@ubuntu:~/workspace/TestDemo/pythonLearn$ git remote
myorigin
@ubuntu:~/workspace/TestDemo/pythonLearn$

用原理图来表示,在git clone前,本地没有git仓库,远程仓库结构如下:
[Git] Git整理(三) 远程仓库和远程分支_第1张图片
git clone后,本地的git仓库如下:
[Git] Git整理(三) 远程仓库和远程分支_第2张图片
当克隆好之后,在本地的操作就和服务器的仓库无关系了,当修改本地代码并提交后,移动的只是本地的master指针(如果处于master分支),服务器的远程分支的origin/master分支不会移动。比如你在本地提交两次之后,Git仓库的结构如下图:
[Git] Git整理(三) 远程仓库和远程分支_第3张图片

在本地仓库中添加远程仓库

使用git remote命令将为本地仓库添加一个远程仓库,这样一来,本地仓库中的代码也就可以推送到远程仓库了,格式如下:

git remote add [branch_name]

branch name 指远程仓库名称,由用户自定义
url指远程仓库的url

比如,我首先使用git init在本地初始化一个仓库:

jiayongqiang@ubuntu:~/Documents/blogs$ git init
Initialized empty Git repository in /home/jiayongqiang/Documents/blogs/.git/

然后我添加添加一个远程仓库:

# 添加一个远程仓库,并在本地命名为origin
jiayongqiang@ubuntu:~/Documents/blogs$ git remote add origin https://github.com/jeraon/blogs.git
jiayongqiang@ubuntu:~/Documents/blogs$ 

最后,我将本地的代码推送到远程分支(git init 时创建了一个master分支):

jiayongqiang@ubuntu:~/Documents/blogs$ git push origin master
Username for 'https://github.com': jeraon
Password for 'https://[email protected]': 
Counting objects: 33, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (33/33), 946.49 KiB | 0 bytes/s, done.
Total 33 (delta 0), reused 0 (delta 0)
To https://github.com/jeraon/blogs.git
 * [new branch]      master -> master
jiayongqiang@ubuntu:~/Documents/blogs$

现在,远程仓库中也将存在master分支了。关于git push我们在下面总结。

了解了git clonegit remote add这两种使用远程仓库的命令后,下面再来看操作几个远程仓库的命令。

1.2.其他远程仓库命令

查看远程仓库

查看远程仓库相关的命令有如下几个:

git remote [show]:显示远程仓库
git ls-remote:显示远程引用列表
git remote -v:显示远程仓库的url,如

@ubuntu:~/workspace/TestDemo$ git remote -v
neworigin	https://github.com/jeraon/TestDemo.git (fetch)
neworigin	https://github.com/jeraon/TestDemo.git (push)

移除远程仓库

git remote re remote_name用于移除远程仓库,如:

@ubuntu:~/workspace/TestDemo$ git remote rm python
@ubuntu:~/workspace/TestDemo$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/sw1
  remotes/origin/sw2
@ubuntu:~/workspace/TestDemo$ git remote
origin
@ubuntu:~/workspace/TestDemo$ 

重命名远程仓库

git remote rename ,如:

@ubuntu:~/workspace/TestDemo$ git remote rename origin neworigin
@ubuntu:~/workspace/TestDemo$ git remote
neworigin
@ubuntu:~/workspace/TestDemo$

拉取远程仓库代码到本地

将远程仓库中最新的代码更新到本地仓库中,有两种方式:git fetchgit pull

git fetch [remote_name]用于拉取远程分支中本地仓库没有的数据,拉取完成后,它并不会自动合入到当前的分支,需要手动进行git merge操作后才会显示出来。

git pull会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支。但前提是必须给当前分支指定一个跟踪分支。

推送本地代码到远程仓库

在对本地进行过数次提交后,则可以通过git push命令将本地代码提交到远程仓库:

git push [] []

remote name指远程仓库名,branch name指本地分支名称

如果指定branch_name,则会在远程仓库中新出现一个branch_name分支,如:

@ubuntu:~/workspace/TestDemo$ git checkout -b branch1 
Switched to a new branch 'branch1'
@ubuntu:~/workspace/TestDemo$ git push origin branch1
Username for 'https://github.com': jeraon
Password for 'https://[email protected]': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/jeraon/TestDemo.git
 * [new branch]      branch1 -> branch1
@ubuntu:~/workspace/TestDemo$ 

关于拉取和推送在远程分支时详细说明。

2.远程分支

远程分支,一般是指远程跟踪分支,它们以remotes/[branch]的形式命名,在本地只有只读权限,不能对远程分支进行移动。一般来说,在git clone时,会自动创建一个origin/master远程分支,表示远程仓库中的master分支,这和git init 时创建master分支类似,可以通过如下几个命令来查看远程分支:

  • git branch -a:列出本地和所有的远程跟踪分支,如:
@ubuntu:~/workspace/TestDemo/TestDemo$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
@ubuntu:~/workspace/TestDemo/TestDemo$ 
  • git branch -r:列出远程仓库中的分支,如:
@ubuntu:~/workspace/TestDemo$ git branch -r
  origin/HEAD -> origin/master
  origin/master
@ubuntu:~/workspace/TestDemo$

2.1.同步远程分支中的更新

使用git fetch

在上面已经说道,如果不和服务器进行连接,本地的提交是不会影响到服务器的,此时本地的Git仓库中结构如下:
[Git] Git整理(三) 远程仓库和远程分支_第4张图片

假设在同一时间,其他人将提交推送到了远程仓库中了,并更新了master分支,此时远程仓库结构表示如下:
[Git] Git整理(三) 远程仓库和远程分支_第5张图片
现在来看,远程仓库中的master分支已经更新了,如果要拉取远程分支中的更新到本地,使用git fetch命令:

@ubuntu:~/workspace/TestDemo$ git fetch origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/jeraon/TestDemo
   2fb01ec..5873073  master     -> origin/master
@ubuntu:~/workspace/TestDemo$

git fetch origin后,会将origin中所有更新和分支的引用拉取到本地,但是并不会显示更新内容,因为fetch只是拉取,并没有merge,所以还需要手动merge:

@ubuntu:~/workspace/TestDemo$ git merge origin/master
Updating 2fb01ec..5873073
Fast-forward
 README.md | 3 +++
 1 file changed, 3 insertions(+)
@ubuntu:~/workspace/TestDemo$

现在,将拉取到的信息merge到了本地当前分支中。

使用git pull

git pull命令相当于以下两条命令:

git fetch
git merge

但前提是当前本地分支必须是一个跟踪分支。因此使用git pull必须首先创建并切换到一个跟踪分支,我们可以使用如下命令先创建一个跟踪origin/master的分支:

@ubuntu:~/workspace/TestDemo$ git checkout -b branch3 remotes/origin/master
Branch branch3 set up to track remote branch master from origin.
Switched to a new branch 'branch3'
@ubuntu:~/workspace/TestDemo$ 

创建好跟踪分支后,就可以使用git pull命令了:

@ubuntu:~/workspace/TestDemo$ git pull origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/jeraon/TestDemo
   5873073..ca95d5a  master     -> origin/master
Updating 5873073..ca95d5a
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
@ubuntu:~/workspace/TestDemo$ 

拉取更新后,本地仓库结构表示如下:
[Git] Git整理(三) 远程仓库和远程分支_第6张图片

2.2.将本地提交推送到远程分支中

git push branch

通过git fetchgit pull可以将远程仓库中的代码更新到本地,如果要将本地的修改推送到远程分支,则使用git push命令,该命令有两种格式:

  • 1 .git push :

该命令实际上会将branch展开为refs/heads/branch:refs/heads/branch,表示“推送本地分支branch来更新远程仓库中的branch分支”。如果没有branch命名的远程分支,则会创建一个新的远程分支branch。比如将本地branch1分支的内容推动到远程仓库中的branch1分支中:

@ubuntu:~/workspace/TestDemo$ git push origin branch1
Username for 'https://github.com': jeraon
Password for 'https://[email protected]': 
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/jeraon/TestDemo.git
   436727e..b20eb4e  branch1 -> branch1
@ubuntu:~/workspace/TestDemo$ 

如果本地仓库中不存在branch分支,则会出错:

@ubuntu:~/workspace/TestDemo$ git push origin branch8
error: src refspec branch8 does not match any.
error: failed to push some refs to 'https://github.com/jeraon/TestDemo.git'
@ubuntu:~/workspace/TestDemo$
  • 2.git push localbranch:remotebranch

该命令表示推送本地的 localbranch 分支,将其作为远程仓库的 remote branch 分支,可以理解为创建一个远程分支,比如创建远程分支branch4并且将当前所在的本地分支推送到远程仓库的branch4分支中:

@ubuntu:~/workspace/TestDemo$ git push origin HEAD:branch4
Username for 'https://github.com': jeraon
Password for 'https://[email protected]': 
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 308 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/jeraon/TestDemo.git
 * [new branch]      HEAD -> branch4
@ubuntu:~/workspace/TestDemo$

当其他人在远程仓库中添加了远程分支后,我们可以使用git fetch拉取到本地,但需要注意的是,当抓取到新的远程跟踪分支时,本地不会自动生成一份可编辑的副本。 换一句话说,这种情况下,不会有一个新的 branch4 分支 - 只有一个不可以修改的 origin/branch4 指针。因此要使用branch4分支,需要进行merge合并到自己分支,或者可以新建一个跟踪分支。

2.3.删除远程分支

如果要从服务器上删除远程分支,使用如下命令:
git push [remote] --delete [branch]
比如删除掉远程仓库origin中除master以外的所有远程分支:

@ubuntu:~/workspace/TestDemo$ git push origin --delete branch1 branch3 branch5 sw1 branch4
Username for 'https://github.com': jeraon
Password for 'https://[email protected]': 
To https://github.com/jeraon/TestDemo.git
 - [deleted]         branch1
 - [deleted]         branch3
 - [deleted]         branch4
 - [deleted]         branch5
 - [deleted]         sw1
@ubuntu:~/workspace/TestDemo$

3.跟踪分支

跟踪分支,也叫做 “上游分支”,是与远程分支有直接关系的本地分支。从一个远程跟踪分支检出一个本地分支会自动创建一个叫做 “跟踪分支”。 如果在一个跟踪分支上输入git pull,Git 能自动地识别去哪个服务器上抓取、合并到哪个分支。当克隆一个仓库时,它通常会自动地创建一个跟踪origin/mastermaster 分支。 当然还可以自己创建跟踪分支。

3.1.创建跟踪分支

创建跟踪分支使用如下命令:
git checkout -b [branch] [remotename]/[branch]

如在本地创建一个branch6分支,作为远程跟踪分支origin/master的跟踪分支:

@ubuntu:~/workspace/TestDemo$ git checkout -b branch6 origin/master
Branch branch6 set up to track remote branch master from origin.
Switched to a new branch 'branch6'
@ubuntu:~/workspace/TestDemo$

还可以给当前的本地分支设置一个跟踪分支,使用如下命令:
git branch -u [remotebranch]或者git branch --set-upstream-to [remotebranch]

如将已存在的本地分支branch7作为origin/master的跟踪分支:

@ubuntu:~/workspace/TestDemo$ git checkout branch7
Switched to branch 'branch7'
@ubuntu:~/workspace/TestDemo$ git branch --set-upstream-to origin/master
Branch branch7 set up to track remote branch master from origin.
@ubuntu:~/workspace/TestDemo$ 

现在,要拉取远程分支origin/master中的内容到本地,就可以在其跟踪分支上使用git pull了:

@ubuntu:~/workspace/TestDemo$ git pull origin
Updating 2fb01ec..ca95d5a
Fast-forward
 README.md | 3 +++
 1 file changed, 3 insertions(+)
@ubuntu:~/workspace/TestDemo$ 

查询分支是否是跟踪分支,可通过git branch -vv命令。

你可能感兴趣的:(Git)