我想获取远程仓库的一个分支(而非全部分支),并创建一个本地跟踪分支,以便跟踪该远程分支的进一步更新。由于远程仓库中的其他分支很大,所以我想避免获取它们。如何做到这一点?
我想获取远程仓库的一个分支(而非全部分支),并创建一个本地跟踪分支,以便跟踪该远程分支的进一步更新。由于远程仓库中的其他分支很大,所以我想避免获取它们。如何做到这一点?
git fetch <remote_name> <branch_name>
对我有效。
git checkout FETCH_HEAD
切换当前分支到最新的拉取(fetch)内容。 - d9kgit fetch <remote>
,则会突然获取整个大型repo。因此最好通过配置来限制提取 - 例如通过git remote set-branches [--add] <name> <branch>…
或(从一开始)通过git clone --single-branch ...
来实现。 - kxrgit remote -v
,它用于列出与当前 Git 仓库相关联的远程仓库的详细信息,包括它们的名称和 URL。 - CervEderror: pathspec '<branch_name>' did not match any file(s) known to git
fatal: A branch named '<branch_name>' already exists.
因此,我根据您的建议提供了这个改进的解决方案。 - ToJofatal: a branch named '<branch_name>' already exists
),则会失败。要了解即使在这种情况下也能正常工作的解决方案,请参见:https://dev59.com/wGw15IYBdhLWcg3w1vOB#70441995。 - ToJo其中一种方法如下:
git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>
这并不会设置跟踪。
要了解更多信息,请参见git fetch文档。
编辑:正如@user1338062在下方注明的那样:如果您还没有想要添加新分支的存储库的本地副本,但是您想要创建一个新的本地存储库,则 git clone --branch <branch_name> --single-branch <repo_url>
提供了一种更简短的解决方案。
git checkout <branchname>
,我会发现如果该名称的分支仅为远程分支,则 git 会自动设置跟踪分支。 - bukzor要将现有的远程仓库更新为仅跟踪特定分支,请使用以下命令:
git remote set-branches <remote-name> <branch-name>
从git help remote
中得知:
set-branches
Changes the list of branches tracked by the named remote. This can be used to track a subset of the available remote branches
after the initial setup for a remote.
The named branches will be interpreted as if specified with the -t option on the git remote add command line.
With --add, instead of replacing the list of currently tracked branches, adds to that list.
--single-branch
的代码库里,后来无法下载其他分支。谢谢! - Yajo一种实现方法:
在 .git/config 中,应将远程仓库的 fetch 设置为获取任何分支:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
获取远程分支:
git fetch origin branch-name
创建一个本地分支 'branch-name' 并使其跟踪远程仓库 origin 上的同名分支。
git checkout -b branch-name origin/branch-name
列出所有分支git branch -a
从作者的帖子中复制:
使用-t
选项来添加git remote add
,例如:
git remote add -t remote-branch remote-name remote-url
-t branch
选项来获取多个分支。git branch -r
检查),则可以通过git fetch <remote_name> <branch_name>
从远程获取特定分支。假设我之前使用--single-branch选项克隆了远程仓库,在这种情况下,我只有一个跟踪分支“cloned”。对于手动调整git配置或键入git remote add <remote_name> <remote_url>
命令的建议让我有点困惑。由于“git remote add”设置了一个新的远程,它显然不能用于现有的远程仓库;提供“-t branch”选项也没有帮助我。git branch -r
检查是否可以将此分支视为跟踪分支。如果不行(像我一样只有单个分支的克隆),则使用"git remote set-branches"与--add选项将此分支添加到跟踪分支列表中:
git remote set-branches --add <remote_name> <branch_name>
2. 从远程获取您所添加的分支:
git fetch <remote_name> <branch_name>
注意:仅在从远程获取了新的跟踪分支后,您才能使用git branch -r
查看它。git checkout --track <remote_name>/<branch_name>
为了完整起见,这里提供一个新的检出命令示例:
git clone --branch gh-pages --single-branch git://github.com/user/repo
如其他答案所述,它设置 remote.origin.fetch
如下:
[remote "origin"]
url = git://github.com/user/repo
fetch = +refs/heads/gh-pages:refs/remotes/origin/gh-pages
[remote "origin"]
fetch = +refs/heads/master:refs/remotes/origin/master
实现该目标的最简单方法
git fetch origin <branch> && git checkout <branch>
示例:我想从源中获取uat分支并将其切换为当前的工作分支。
git fetch origin uat && git checkout uat