如何只获取远程 Git 存储库的一个分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6368987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fetch only one branch of a remote Git repository?
提问by mellis
I'd like to grab a single branch (not all of them) of a remote repository and create a local tracking branch that can track further updates to that remote branch. The other branches in the remote repository are very big, so I'd like to avoid fetching them. How do I do this?
我想获取远程存储库的一个分支(不是全部)并创建一个本地跟踪分支,该分支可以跟踪对该远程分支的进一步更新。远程存储库中的其他分支非常大,所以我想避免获取它们。我该怎么做呢?
Edit: I figured this out myself but StackOverflow refuses to let me provide the answer as an answer so I'll put it here in the question instead.
编辑:我自己想通了这一点,但 StackOverflow 拒绝让我提供答案作为答案,所以我将其放在问题中。
You use the -t option to git remote add, e.g.:
您使用 -t 选项来 git remote add,例如:
git remote add -t remote-branch remote-name remote-url
You can use multiple "-t branch
" options to grab multiple branches.
您可以使用多个“ -t branch
”选项来获取多个分支。
回答by Abdulsattar Mohammed
git fetch <remote_name> <branch_name>
Worked for me.
对我来说有效。
回答by hcs42
One way is the following:
一种方法如下:
git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>
This does not set up tracking though.
但这并没有设置跟踪。
For more information, see the documentation of git fetch.
有关更多信息,请参阅git fetch 的文档。
EDIT: As @user1338062 notes below: if you don't already have a local clone of the repository where you want to add the new branch, but you want to create a fresh local repository, then the git clone --branch <branch_name> --single-branch <repo_url>
provides a shorter solution.
编辑:正如@user1338062在下面指出的那样:如果您还没有要在其中添加新分支的存储库的本地克隆,但是您想创建一个新的本地存储库,则git clone --branch <branch_name> --single-branch <repo_url>
提供了一个更短的解决方案。
回答by MrMadsen
I know there are a lot of answers already, but these are the steps that worked for me:
我知道已经有很多答案,但这些是对我有用的步骤:
git fetch <remote_name> <branch_name>
git branch <branch_name> FETCH_HEAD
git checkout <branch_name>
git fetch <remote_name> <branch_name>
git branch <branch_name> FETCH_HEAD
git checkout <branch_name>
These are based on the answer by @Abdulsattar Mohammed, the comment by @Christoph on that answer, and these other stack overflow questions and their answers:
这些基于@Abdulsattar Mohammed 的答案、@Christoph 对该答案的评论,以及这些其他堆栈溢出问题及其答案:
回答by Ikar Pohorsky
To update existing remote to track specific branches only use:
要更新现有遥控器以跟踪特定分支,请仅使用:
git remote set-branches <remote-name> <branch-name>
From git help remote
:
来自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.
回答by klode
One way to do it:
一种方法:
in .git/config fetch for the remote repo should be set to fetch any branch:
在 .git/config 中,远程仓库的 fetch 应该设置为获取任何分支:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
to fetch the remote branch:
获取远程分支:
git fetch origin branch-name
to create a local branch 'branch-name' set up to track remote branch 'branch-name' from origin.
创建一个本地分支“branch-name”,设置为从源跟踪远程分支“branch-name”。
git checkout -b branch-name origin/branch-name
to list all branches
列出所有分支
git branch -a
回答by astronaut
Copied from the author's post:
复制自作者的帖子:
Use the -t
option to git remote add
, e.g.:
使用-t
选项git remote add
,例如:
git remote add -t remote-branch remote-name remote-url
You can use multiple -t branch
options to grab multiple branches.
您可以使用多个-t branch
选项来获取多个分支。
回答by ColinM
If you want to change the default for "git pull" and "git fetch" to only fetch specific branches then you can edit .git/config so that the remote config looks like:
如果您想更改“git pull”和“git fetch”的默认值以仅获取特定分支,那么您可以编辑 .git/config 以便远程配置如下所示:
[remote "origin"]
fetch = +refs/heads/master:refs/remotes/origin/master
This will only fetch master from origin by default. See for more info: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
默认情况下,这只会从 origin 获取 master。有关更多信息,请参阅:https: //git-scm.com/book/en/v2/Git-Internals-The-Refspec
EDIT: Just realized this is the same thing that the -t option does for git remote add. At least this is a nice way to do it after the remote is added if you don't want ot delete the remote and add it again using -t.
编辑:刚刚意识到这与 -t 选项对 git remote add 的作用相同。如果您不想删除遥控器并使用 -t 再次添加它,那么至少这是在添加遥控器后执行此操作的好方法。
回答by user1338062
For the sake of completeness, here is an example command for a fresh checkout:
为了完整起见,这里是一个用于全新结账的示例命令:
git clone --branch gh-pages --single-branch git://github.com/user/repo
As mentioned in other answers, it sets remote.origin.fetch
like this:
正如其他答案中提到的,它设置remote.origin.fetch
如下:
[remote "origin"]
url = git://github.com/user/repo
fetch = +refs/heads/gh-pages:refs/remotes/origin/gh-pages
回答by scottalan
git version: 2.74
git 版本:2.74
This is how I do it:
这就是我的做法:
git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]
回答by Karthikeyan Mohan
git version 2.16.1.windows.4
git 版本 2.16.1.windows.4
Just doing a git fetch remoteRepositoryName branchName(eg: git fetch origin my_local_branch)
is enough. Fetch will be done and a new local branch will be created with the same name and tracking will be set to remote branch.
只需执行git fetch remoteRepositoryName branchName(eg: git fetch origin my_local_branch)
就足够了。将完成提取并创建一个具有相同名称的新本地分支,并将跟踪设置为远程分支。
Then perform git checkout branchName
然后执行git checkout branchName