远程存储库包含各种分支,如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 fetch origin <branchName>

移动到该分支:

git checkout <branchName>

其他回答

要签出远程而非本地存在的myBranch,这对我很有用:

git fetch --all
git checkout myBranch

我收到了这条消息:

Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'

我键入了

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 fetch&&git checkout<你朋友的分行名>应该可以做到这一点

我想给您一个命令行,用于将所有远程分支提取到本地并切换到所需的新创建的本地分支:

git fetch && git checkout discover

运行上述命令后,您将收到以下消息:

Switched to a new branch 'discover'
Branch discover set up to track remote branch discover from origin.

第一行表示切换到新分支-为什么新?它已经在远程!

但实际上你也必须在本地创建它。该分支从远程索引中获取,并在本地为您创建。

这里的discover是从存储库的远程分支discover创建的一个新分支。

但第二行比第一行提供了更多信息,告诉我们:

我们的分支机构旨在跟踪同名的远程分支机构。

尽管gitfetch会将所有分支获取到本地。但如果您在git分支之后运行它,您将只看到本地的master分支。为什么?

因为对于远程中的每个分支,您也必须在本地创建它,以便将其作为gitcheckout<branchname>进行跟踪,正如我们在上面的示例中所做的那样。

运行gitcheckout命令后,您可以运行gitbranch,现在您可以看到这两个分支:

master和2。在本地列表中发现。

假设你的遥控器git@xyz.git你想要它的random_branch分支。流程应如下:

首先检查遥控器列表git远程-v如果你没有git@xyz.git在上述命令的输出中,您可以通过git远程添加xyzgit@xyz.git现在,您可以通过git获取xyz现在通过git checkout-b my_copy_random_branch xyz/random_brance检查分支列表查看所有的分支

本地分支my_copy_random_branch将跟踪远程的random_brance分支。