远程存储库包含各种分支,如origin/davis_branch:

$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master

如何将daves_branch获取到本地存储库,以便它跟踪origin/davis_branch?

我尝试了:

$ git fetch origin discover
$ git checkout discover

当前回答

使用此简单命令:

git checkout -b 'your_branch' origin/'remote branch'

其他回答

我键入了

git checkout <branch_name>

并且得到

Branch <branch_name> set up to track remote branch <branch_name> from origin.
Switched to a new branch '<branch_name>'

有时,你被要求不要摆弄主分支,只在远程分支工作(正如我被要求的那样)。所以你只需要远程分支。

因此,要单独克隆远程分支(没有主分支),请执行以下操作

git clone url --branch remote_branch_name

哪里remote_branch_name是远程分支的名称

例如

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15

这将确保您使用远程分支的名称将远程分支克隆到本地分支。

现在,如果您提交代码并推送,代码将单独提交给该分支。

使用此简单命令:

git checkout -b 'your_branch' origin/'remote branch'

使用gitbranch-a(本地和远程分支)或gitbranch-r(仅远程分支)查看所有远程及其分支。然后,您可以对远程执行git checkout-t remotes/repo/branch并创建本地分支。

还有一个git-ls-remote命令,用于查看该远程的所有ref和标记。

在我看来,这一行命令是获取和签出本地不存在的远程分支的最简单方法:git获取和git签出remote_branch