git图形化工具GitKraken的使用——创建远程分支与建立追踪关系

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mr_wuch/article/details/73506145

前面的操作都是基于本地仓库、本地分支进行的,接下来要介绍远程操作的一些东西。

远程仓库

# 增加一个新的远程仓库,并命名
git remote add <remote-name> <url>

# 下载远程仓库的所有变动
git fetch <remote-name>

# 显示所有远程仓库
git remote -v

# 显示某个远程仓库的信息
git remote show <remote-name>

创建远程分支

# 列出所有远程分支
git branch -r

# 列出所有本地分支和远程分支
git branch -a

# 列出所有本地分支
git branch

在第一篇文章 : git图形化工具GitKraken的使用——初始化项目 中写过创建远程分支,下面详细介绍下创建分支并建立追踪关系的情景

情景一:远端并不存在develop分支(这里假设用develop模拟)

也就是第一次创建的分支,我们可以,在本地创建分支,然后push到远端:

# 创建分支并切换
$ git checkout -b develop
Switched to a new branch 'develop'

# 推送到远端,会在远端创建同名的分支,-u参数会创建追踪关系
$ git push -u origin develop
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/wuchongzi/gitFlowTset.git
 * [new branch]      develop -> develop
Branch develop set up to track remote branch develop from origin.

情景二:远端已经有了develop分支,在本地创建一样的分支并与远端建立追踪关系

这个情景就假如说你先经历了情景一,然后你换了台电脑,或者另一个小伙伴克隆远端仓库,clone默认只会克隆下master分支,现在另一个小伙伴需要在develop分支上开发,而远端已经存在了develop分支。

现在在情景二下还得分两种情况:

1、假如另一小伙伴是第一次clone项目

# 克隆
git clone <url>

clone完成之后查看下远程的仓库信息:

$ git remote show origin
Enter passphrase for key '/c/Users/lenovo/.ssh/id_rsa':
* remote origin
  Fetch URL: [email protected]:wuchongzi/test.git
  Push  URL: [email protected]:wuchongzi/test.git
  HEAD branch: master
  Remote branches:
    develop tracked
    master  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

develop已经被tracked了,他直接创建本地develop分支并建立关系就OK了:

# 创建切换本地分支并与远端分支建立追踪关系(或者叫检出远程分支)
git checkout -b develop origin/develop
# 或者使用下面的命令,两个是一样的道理
git checkout --track origin/develop # --track可以简写为-t

2、假如另一个小伙伴在你创建develop分支之前已经clone了项目到自己的电脑

这时候如果他执行 git checkout -b develop origin/develop,就会失败:

$ git checkout -b develop origin/develop
fatal: Cannot update paths and switch to branch 'develop' at the same time.
Did you intend to checkout 'origin/develop' which can not be resolved as commit?

为什么会失败呢,远端明明已经有了develop分支,这时候他查看远端的分支:

扫描二维码关注公众号,回复: 3861283 查看本文章
$ git branch -r
  origin/HEAD -> origin/master
  origin/master

然而并没有看到远程的develop分支,然后他查看了远程仓库信息:

$ git remote show origin
Enter passphrase for key '/c/Users/lenovo/.ssh/id_rsa':
* remote origin
  Fetch URL: [email protected]:wuchongzi/test.git
  Push  URL: [email protected]:wuchongzi/test.git
  HEAD branch: master
  Remote branches:
    develop new (next fetch will store in remotes/origin)
    master  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

他会发现这里develop没有tracked,显示的是new,后面有一句话 next fetch will store in remotes/origin,也就是develop并没有被追踪,这是因为git并没有每一次都从远程更新仓库信息,需要我们提前手动更新:

$ git remote update
Fetching origin
Enter passphrase for key '/c/Users/lenovo/.ssh/id_rsa':
From github.com:wuchongzi/test
 * [new branch]      develop    -> origin/develop

然后执行 git fetch 无任何输出(成功),现在再来看一下远程仓库信息:

$ git remote show origin
Enter passphrase for key '/c/Users/lenovo/.ssh/id_rsa':
* remote origin
  Fetch URL: [email protected]:wuchongzi/test.git
  Push  URL: [email protected]:wuchongzi/test.git
  HEAD branch: master
  Remote branches:
    develop tracked
    master  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

OK,现在develop和master一样,都被tracked了,下面执行 git checkout -b develop origin/develop 就成功了:

$ git checkout -b develop origin/develop
Switched to a new branch 'develop'
Branch develop set up to track remote branch develop from origin. # 这句话的意思就是本地的develop分支已经与远端的develop分支建立了追踪关系

在GitKraken上也可以手动更新远端仓库信息:

这里写图片描述

追踪关系

前面一直提到的建立追踪关系,这里有两个专用名词叫做远程跟踪分支和跟踪分支,刚开始对这两者实在是理解不了,后来将这篇文章:Git 分支 - 远程分支 读了三遍才有所理解,下面我说下我的理解,可能有说的不对的地方,希望大家给我提出来

首先是远程分支

远程分支(remote branch),简单来说就是在远程仓库的普通分支,远程仓库就像是另一台的电脑的本地仓库,远程分支就是他下面的一个分支,和我们在本地创建的分支是一样的道理。

官方的话是这样的:

远程引用是对远程仓库的引用(指针),包括分支、标签等等。

通过 git ls remote <remote-name> 可以显示地获取远程引用的完整列表:

$ git ls-remote origin
0f510950ffb8413ae7deada90745c707854364aa    HEAD
0f510950ffb8413ae7deada90745c707854364aa    refs/heads/master

在前面也提到过,用 git remote show <remote-name> 可以获取远程分支的更多信息:

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/wuchongzi/test2.git
  Push  URL: https://github.com/wuchongzi/test2.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

远程跟踪分支

远程跟踪分支(remote/branch),是远程分支状态的引用。他们是你不能移动的本地引用,当你做任何网络通信操作时,他们会自动移动。远程跟踪分支像是你上次连接到远程仓库时(pull,push之类的操作),那些分支所处状态的书签。

简单来说,在我们从远程服务器clone的时候,git的clone命令会为你自动将其命名为origin,拉取它(远程)所有的数据,创建一个指向它(远程)的master分支的指针,并且在本地将其命名为 origin/master,git也会给你一个与origin的master分支指向同一个地方的本地master分支,这样你就有了工作基础。

git remote show origin 你就可以看到远程的分支名字是master而不是origin/master,这就是远程分支和远程跟踪分支的区别。

在上面情景二的两种情况,就是有无远程跟踪分支的区别:

  • 情况一是远程已经有了develop分支,所以clone的时候git拉取远程所有的数据,创建了指向远程的master分支和develop分支,也就是远程跟踪分支 origin/masterorigin/develop,所以可以直接使用 git checkout -b develop origin/develop

  • 情况二是先clone得仓库,后有的develop远程分支,所以这时候,本地并没有origin/develop这个远程跟踪分支,所以要和远程进行一次通信同步,这样就会在本地创建origin/develop远程跟踪分支,才能进行下一步操作

跟踪分支又是什么

跟踪分支(tracking branch),从一个远程跟踪分支检出的本地分支便是跟踪分支,该本地分支对应的远程跟踪分支称为上游(Upstream)分支,跟踪分支是与远程分支有直接关系的本地分支。

clone一个仓库的时候,会自动创建一个跟踪origin/master的master分支。

在上面的情景二中的命令 git checkout -b develop origin/develop 就是检出了一个跟踪分支。

设置已有的本地分支跟踪一个刚刚拉取下来的远程分支

设置已有的本地分支跟踪一个刚刚拉取下来的远程分支,或者想要修改正在跟踪的上游分支,你可以在任意时间使用 -u--set-upstream-to(这里新版git已经改为–set-upstream-to而不是–set-upstream,要注意一下,如果你使用–set-upstream,git会提示你这个参数将要弃用) 选项运行 git branch 来显式地设置:

# 首先要checkout到要修改的分支上,然后执行
$ git branch -u origin/feature
# 等价于:
$ git branch --set-upstream-to=origin/feature

# 或者不用切换,可以执行
$ git branch -u origin/feature feature
# 等价于:
$ git branch --set-upstream-to=origin/feature feature

可以使用 git branch 的 -vv 选项来查看设置的所有跟踪分支:

$ git branch -vv
* develop  e0e8353 [origin/develop] Initial commit
  feature1 e0e8353 [origin/feature1] Initial commit
  feature2 e0e8353 [origin/feature2] Initial commit
  feature3 e0e8353 [origin/feature3] Initial commit
  master   e0e8353 [origin/master] Initial commit

参考文章

Git 分支 - 远程分支

猜你喜欢

转载自blog.csdn.net/mr_wuch/article/details/73506145
今日推荐