远程存储库包含各种分支,如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获取远程分支我的同事怎么能具体地拉动那个分支。

如果问题是,我如何才能使用远程分支,或者我如何Git签出远程分支?,更简单的解决方案是:

使用Git(>=1.6.6),您可以使用:

git checkout <branch_name>

如果找不到本地<branch_name>,但恰好在一个远程中存在具有匹配名称的跟踪分支,请将其视为等同于:

git checkout -b <branch_name> --track <remote>/<branch_name>

参见Git签出文档

对于您的朋友:

$ git checkout discover
Branch discover set up to track remote branch discover
Switched to a new branch 'discover'

其他回答

Use:

git checkout -b serverfix origin/serverfix

这是一个非常常见的操作,Git提供了--track速记:

git checkout --track origin/serverfix

事实上,这是如此普遍,甚至有一条捷径。如果您尝试签出的分支名称(a)不存在,并且(b)仅与一个远程服务器上的名称完全匹配,Git将为您创建一个跟踪分支:

git checkout serverfix

要使用与远程分支不同的名称设置本地分支,可以轻松地使用具有不同本地分支名称的第一个版本:

git checkout -b sf origin/serverfix

现在,您的本地分支sf将自动从origin/serverfix中提取。

来源:Pro Git,第二版,由Scott Chacon和Ben Straub撰写(为便于阅读而删减)

如果要获取所有远程分支,请键入:

git fetch --all

步骤如下:;

gitfetchorigin或gitfetch-all,这将把所有远程分支提取到本地,然后这是您可以使用的第二个选项。git checkout--跟踪原点/<要切换的The_remote_branch>

然后处理这个分支,您可以通过键入

git branch

它显示您当前所在的分支。

如果您有一个使用--depth 1克隆的存储库,那么列出的许多命令将无法工作。例如,请参见此处

% git clone --depth 1 https://github.com/repo/code
Cloning into 'code'...
cd code
remote: Counting objects: 1778, done.
remote: Compressing objects: 100% (1105/1105), done.
remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0
Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done.
Checking out files: 100% (1215/1215), done.
% cd code
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
% git fetch origin other_branch
remote: Counting objects: 47289, done.
remote: Compressing objects: 100% (15906/15906), done.
remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0
Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done.
Resolving deltas: 100% (30151/30151), completed with 362 local objects.
From https://github.com/repo/code
 * branch            other_branch-> FETCH_HEAD
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
%

在这种情况下,我会重新克隆存储库,但可能还有其他技术,例如git浅层克隆(clone--depth)错过了远程分支

git fetch

git branch -r

git checkout <branch_name>