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

远程仓库

概述

远程仓库是指托管在其他服务器上或网络上的项目版本库,使用git init会创建一个本地仓库,如果要使用远程仓库,则通过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 clone后,本地git仓库如下:
这里写图片描述
当克隆好之后,在本地的操作就和服务器的仓库无关系了,当修改本地代码并提交后,移动的只是本地的master指针(如果处于master分支),服务器的远程分支的origin/master分支不会移动。比如你在本地提交两次之后,Git仓库的结构如下图:
图2

操作远程仓库

1将远程仓库中的代码更新到本地仓库代码

将远程仓库中最新的代码更新到本地仓库中,有两种方式:git fetchgit pull
git fetch [remote_name]用于拉取远程分支中本地仓库没有的数据,拉取完成后,它并不会自动合入到当前的分支,需要手动进行git merge操作后才会显示出来。
git pull会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支。但前提是必须给当前分支指定一个跟踪分支。
这两个命令在远程分支时详细说明。

2.推送到远程仓库

git push [remotename] [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$ 

这个命令在远程分支时详细说明。

3.查看远程仓库

查看远程仓库相关的命令有如下几个:
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)

4.添加远程仓库

git remote add [branch_name] <url>:该命令用于在本地仓库中添加一个远程仓库,当添加之后,就可以将添加的远程仓库中的代码fetch到本地仓库中了,如:

@ubuntu:~/workspace/TestDemo$ git remote add python https://github.com/jeraon/pythonLearn.git
origin
python
@ubuntu:~/workspace/TestDemo$  git fetch python
warning: no common commits
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 25 (delta 4), reused 20 (delta 2), pack-reused 0
Unpacking objects: 100% (25/25), done.
From https://github.com/jeraon/pythonLearn
 * [new branch]      master     -> python/master

5.移除远程仓库

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$ 

6.重命名远程仓库

git remote rename <remote_name> <newname>,如:

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

Git 远程分支

概述

远程分支,一般是指远程跟踪分支,它们以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$

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

git fetch <remotename>

在上面已经说道,如果不和服务器进行连接,本地的提交是不会影响到服务器的,此时本地的Git仓库中结构如下:
这里写图片描述

假设在同一时间,其他人将提交推送到了远程仓库中了,并更新了master分支,此时远程仓库结构表示如下:
这里写图片描述
现在来看,远程仓库中的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 <remotename>

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

git fetch remotename
git merge remotebranch

但前提是当前本地分支必须是一个跟踪分支。因此使用git pull必须首先创建并切换到一个跟踪分支:

@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$ 

拉取更新后,本地仓库结构表示如下:
这里写图片描述

2.将本地代码推送到远程分支中

git push <remotename> branch

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

  • 1 .git push origin branch:
    该命令实际上会将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 origin localbranch:remotebranch:
    该命令表示推送本地的 localbranch 分支,将其作为远程仓库的 remotebranch 分支,可以理解为创建一个远程分支,比如创建远程分支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$

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

3.跟踪分支

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

创建跟踪分支

创建跟踪分支使用如下命令:
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]
比如将已存在的本地分支branch6和branch7作为origin/master的跟踪分支:

@ubuntu:~/workspace/TestDemo$ git checkout branch6 
Switched to branch 'branch6'
@ubuntu:~/workspace/TestDemo$ git branch -u origin/master
Branch branch6 set up to track remote branch master from origin.
@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$ 

4.删除远程分支

如果要从服务器上删除远程分支,使用如下命令:
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$

猜你喜欢

转载自blog.csdn.net/fightfightfight/article/details/80245791