远程存储库包含各种分支,如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
远程存储库包含各种分支,如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>
其他回答
我想给您一个命令行,用于将所有远程分支提取到本地并切换到所需的新创建的本地分支:
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。在本地列表中发现。
如果您有一个使用--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)错过了远程分支
要签出远程而非本地存在的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 clone<repo_url>-b<branch>(仅克隆特定分支)的存储库,则应修改<repo_name>/.git/config文件。替换或修改引用[remote“origin”]部分的fetch目标的行,让命令git fetch--all发现所有分支:
[remote "origin"]
url = <repo_git_url>
fetch = +refs/heads/master:refs/remotes/origin/master
确保将提取参数点设置为/heads/master。
小心git fetch,因为这会获取所有数据,所以可能需要很长时间。
[快速回答]
有很多选择,我最喜欢的是:
-备选方案1:
git fetch --all
git checkout YourBranch
使用此替代方法时,请使用远程存在但不在本地的分支。
-备选方案2:
git checkout -b 'YourBranch' origin/'YourRemote'
也许,这是最简单的方法。