Git 如何 clone 非 master 分支的代码

问题描述

我们每次使用命令

git clone git@gitlab.xxx.com:xxxxx.git

默认 clone 的是这个仓库的 master 分支。如果最新的代码不在 master 分支上,该如何拿到呢?如下图所示,最新的代码可能在 daily/1.4.1 分支上,我们希望拿到这个分支上的代码。

Git 如何 clone 非 master 分支的代码

解决方案

我们在本地先建立一个分支,建议名称和远程的想要同步的分支名称一样。

git branch daily/1.4.1

在切换到这个本地分支

git checkout daily/1.4.1
# Switched to branch 'daily/1.4.1'

接下来就可以去建立上游分支的关联了,但是这个命令比较长,不好记,我们可以直接先 pull 一下,git 会提示我们相应的操作和命令。

git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> daily/1.4.1

我们看到最后一行,执行这个命令,即可完成与上游分支的关联。

git branch --set-upstream-to=origin/daily/1.4.1 daily/1.4.1
# Branch daily/1.4.1 set up to track remote branch daily/1.4.1 from origin.

然后再 pull 一下就好了!

git pull

更多 Git 教程系列文章: 

Git 的详细介绍:请点这里
Git 的下载地址:请点这里

相关推荐