我想获取远程存储库的单个分支(不是所有分支),并创建一个本地跟踪分支,该分支可以跟踪对该远程分支的进一步更新。远程存储库中的其他分支非常大,所以我希望避免获取它们。我怎么做呢?
当前回答
摘自作者的文章:
使用-t选项来远程添加git,例如:
git remote add -t remote-branch remote-name remote-url
您可以使用多个-t分支选项来获取多个分支。
其他回答
这个方法对我很有用。
获取目标分支的远程分支:
git fetch origin branch-name
签出目标分支:
git checkout -b branch-name origin/branch-name
在这里,我尝试成功地获取release-20.10.08。
name:directory zgong$ git fetch release-20.10.04 release-20.10.04
fatal: 'release-20.10.04' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git fetch origin release-20.10.04
From ssh://stash.trusted.visa.com:7999/vdcbc3a/vmcp-android-mobile-app
* branch release-20.10.04 -> FETCH_HEAD
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git checkout -b release-20.10.08 origin/release-20.10.08
fatal: 'origin/release-20.10.08' is not a commit and a branch 'release-20.10.08' cannot be created from it
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git fetch origin release-20.10.08
remote: Counting objects: 637, done.
remote: Compressing objects: 100% (320/320), done.
remote: Total 637 (delta 303), reused 465 (delta 202)
Receiving objects: 100% (637/637), 312.26 KiB | 262.00 KiB/s, done.
Resolving deltas: 100% (303/303), done.
From ssh://stash.trusted.visa.com:7999/vdcbc3a/vmcp-android-mobile-app
* branch release-20.10.08 -> FETCH_HEAD
* [new branch] release-20.10.08 -> origin/release-20.10.08
WM-C02WM0T3HTD8:vdca_android_20_10_04_stable zgong$ git checkout -b release-20.10.08 origin/release-20.10.08
M VMCP/fmcore_android
M VMCP/foundation_android
M VMCP/mep_3ds_android
M VMCP/mep_login_android
M VMCP/mep_provisioning_and
Branch 'release-20.10.08' set up to track remote branch 'release-20.10.08' from 'origin'.
Switched to a new branch 'release-20.10.08'
为了完整起见,这里有一个新签出的示例命令:
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
让我用我的两便士来曲解马德森先生的回答:
Git获取<remote_name_or_url> <branch_name> git checkout FETCH_HEAD -B <branch_name>
与马德森的提议相比,这两条线路的主要优势在于,即使分支机构已经在本地存在,它也能工作。
要更新现有的远程跟踪特定的分支,只使用:
git remote set-branches <remote-name> <branch-name>
从git帮助远程:
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.
如果你想改变默认的“git pull”和“git fetch”只获取特定的分支,那么你可以编辑.git/config,使远程配置看起来像这样:
[remote "origin"]
fetch = +refs/heads/master:refs/remotes/origin/master
默认情况下,这只会从原点获取master。 查看更多信息:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
编辑:刚刚意识到这和-t选项对git远程添加所做的事情是一样的。至少,如果你不想在远程添加后删除远程并使用-t再次添加远程,这是一个很好的方法。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别