我想获取远程存储库的单个分支(不是所有分支),并创建一个本地跟踪分支,该分支可以跟踪对该远程分支的进一步更新。远程存储库中的其他分支非常大,所以我希望避免获取它们。我怎么做呢?
当前回答
git fetch <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
答案取决于你想要完成什么。
If it is a one time deal from a different repository and you don't need a reference (e.g. to merge GitHub Pull Requests, where <remote_url> is like https://github.com/USER/REPO.git), then you can use: git checkout -b <local_branch> <local_branch_to merge_into> git pull <remote_url> <remote_branch> If you want to update and track the branch you have to set first the remote and there are 4 alternatives: If you are cloning a new repository (e.g. working only on it) git clone --single-branch --branch remote_branch remote_url If you are adding a new remote to your working directory # multiple -t options are allowed git remote add -t <remote_branch> <remote_repo> <remote_url> If you are adding the branch restriction to an existing remote in your working directory # with --add it will add the branch instead of setting it # you can add multiple branches with multiple --add lines # wildcards are allowed, # e.g. branch_v\* matching branch_v1, branch_v2, ... git remote set-branches [--add] <remote_repo> <remote_branch> You could also skip the restrictions because clone by default fetches only the main branch and remote add does not fetch branches. Buth then you'll have to mention the remote branch all the remote branch all the time you fetch the remote_repo. git remote add <remote_repo> <remote_url> After setting the remote you can fetch the remote branch, checkout and pull: # If you set only one <remote_branch> in the restrictions above (i.e no option 4), # then you can omit it and still only <remote_branch> will be fetched git fetch <remote_repo> [<remote_branch>] # without -b the local branch name is guessed to be the same as the remote one git checkout --track [-b <local_branch>] <remote_repo>/<remote_branch>
检查远程和已经或将要获取的分支的最佳命令是git remote show <remote_repo>。它会打印“Remote branch:”下的分支列表,还会告诉你它们是否被获取,以及是否被跟踪。
你也可以通过使用git branch -r列出已知的远程分支来检查远程分支的限制,如果你有很多远程,可以结合grep,或者通过检查git配置文件.git/config中的远程详细信息。它将包含如下部分:
[remote "<remote_repo>"]
url = <remote_url>
fetch = +refs/heads/<remote_branch>:refs/remotes/<remote_repo>/<remote_branch>
编辑配置文件将改变限制,但我同意@alexk的意见,这不是一个好主意。
NOTE: If a branch is not in the list of branches of a remote (visible in git remote show or the config file), then you will not be able to have a reference to it, git will save it to the temporary FETCH_HEAD and you will not be able to track it or to use it directly in git checkout. This is the problem that brought me to this thread (the opposite of the one in the question): I cloned a repo with GitHub client gh repo clone USER/REPO and it added automatically "upstream", the repository forked from, restricted only to the branch "master". I was unable to checkout other branches and getting errors like "fatal: '<remote_repo>/<remote_branch>' is not a commit and a branch '<local_branch>' cannot be created from it". I fixed it with: git remote set-branches <remote_repo> \*.
在我的例子中,我想在不创建新的远程的情况下获取一个分支,所以这是有效的:
Git获取<远程url> <远程分支名称>:<本地分支名称>
我知道已经有很多答案了,但下面这些步骤对我来说是有效的:
Git获取<remote_name> <branch_name> git branch <branch_name> FETCH_HEAD . git Git checkout <branch_name>
这些是基于@Abdulsattar Mohammed的回答,@Christoph对这个回答的评论,以及其他堆栈溢出问题和他们的答案:
如何签出远程Git分支? Git中的FETCH_HEAD是什么意思?
答案实际上取决于当前的跟踪分支列表。你可以使用git fetch <remote_name> <branch_name>从remote获取特定的分支,前提是该分支已经在跟踪分支列表中(你可以使用git branch -r检查它)。
让我们假设我之前用——single-branch选项克隆了远程,在这种情况下,我拥有的唯一跟踪分支就是“克隆”的分支。我有点困惑的建议手动调整git配置,以及键入git remote add <remote_name> <remote_url>命令。因为“git remote add”设置了一个新的远程,它显然不能与现有的远程存储库一起工作;提供“-t branch”选项对我没有帮助。
如果远程存在,并且你想要获取的分支存在于该远程:
使用git branch -r检查是否可以将该分支视为跟踪分支。如果不是(就像我的例子中一个分支克隆),通过“git remote set-branches”添加这个分支到跟踪分支列表中,带——add选项:
Git远程设置分支——add <remote_name> <branch_name>
从远程获取你已经添加的分支:
Git获取<remote_name> <branch_name> 注意:只有在从远程获取新的跟踪分支后,你才能在跟踪分支列表中使用git branch -r看到它。
使用"checkout——track"创建并签出一个新的本地分支,它的"branch_name"将与跟踪分支相同:
Git checkout——track <remote_name>/<branch_name> . txt
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别