远程存储库包含各种分支,如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 remote
=> One
=> Two

并且您知道要签出的分支名称,例如br1.2.3.4,然后执行

git fetch One
=> returns all meta data of remote, that is, the branch name in question.

剩下的就是结账

git checkout br.1.2.3.4

然后用它做新的树枝。

其他回答

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

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

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 pull origin your_branch_name

要签出远程而非本地存在的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>'