我想获取远程存储库的单个分支(不是所有分支),并创建一个本地跟踪分支,可以跟踪对该远程分支的进一步更新.远程存储库中的其他分支非常大,所以我想避免获取它们.我该怎么做呢?
编辑:我自己想出来了,但StackOverflow拒绝让我提供答案作为答案,所以我会把它放在问题中.
你使用-t选项来git远程添加,例如:
git remote add -t remote-branch remote-name remote-url
Run Code Online (Sandbox Code Playgroud)
您可以使用多个" -t branch"选项来获取多个分支.
Abd*_*med 187
git fetch <remote_name> <branch_name>
Run Code Online (Sandbox Code Playgroud)
为我工作.
hcs*_*s42 62
一种方法如下:
git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>
Run Code Online (Sandbox Code Playgroud)
但这并没有设置跟踪.
有关更多信息,请参阅git fetch的文档.
编辑:正如@ user1338062 所述:如果您还没有要添加新分支的存储库的本地克隆,但是您想要创建一个新的本地存储库,则git clone --branch <branch_name> --single-branch <repo_url>提供更短的解决方案.
Ika*_*ský 23
要更新现有远程以跟踪特定分支,仅使用:
git remote set-branches <remote-name> <branch-name>
Run Code Online (Sandbox Code Playgroud)
来自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.
Run Code Online (Sandbox Code Playgroud)
MrM*_*sen 23
我知道已经有很多答案,但这些步骤对我有用:
git fetch <remote_name> <branch_name>git branch <branch_name> FETCH_HEADgit checkout <branch_name>这些是基于@Abdulsattar Mohammed的答案,@ Christoph对该答案的评论,以及其他堆栈溢出问题及其答案:
klo*_*ode 19
一种方法:
在.git/config远程仓库的提取应设置为获取任何分支:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)
获取远程分支:
git fetch origin branch-name
Run Code Online (Sandbox Code Playgroud)
创建一个本地分支'branch-name'设置为从源跟踪远程分支'branch-name'.
git checkout -b branch-name origin/branch-name
Run Code Online (Sandbox Code Playgroud)
列出所有分支
git branch -a
Run Code Online (Sandbox Code Playgroud)
ast*_*aut 17
复制自作者的帖子:
使用-t选项git remote add,例如:
git remote add -t remote-branch remote-name remote-url
Run Code Online (Sandbox Code Playgroud)
您可以使用多个-t branch选项来获取多个分支.
Ale*_*exK 12
答案实际上取决于您拥有的当前跟踪分支列表。git fetch <remote_name> <branch_name> 只有当分支已经在跟踪分支列表中时,您才能从远程获取特定分支(您可以使用 进行检查git branch -r)。
假设我之前已经使用 --single-branch 选项克隆了遥控器,在这种情况下,我拥有的唯一一个跟踪分支是“克隆”分支。我对手动调整 git config 以及键入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>git fetch <remote_name> <branch_name>
注意:只有从远程获取新的跟踪分支后,您才能在跟踪分支列表中看到它git branch -r。git checkout --track <remote_name>/<branch_name>ToJ*_*oJo 12
让我用我的两便士对马德森先生的回答进行一些改动:
git fetch <remote_name_or_url> <branch_name>git checkout FETCH_HEAD -B <branch_name>与MrMadsen 的提议相比,这两条线路的主要优点是,即使本地已经存在分支机构,它也可以工作。
Col*_*inM 10
如果要将"git pull"和"git fetch"的默认值更改为仅获取特定分支,则可以编辑.git/config以使远程配置如下所示:
[remote "origin"]
fetch = +refs/heads/master:refs/remotes/origin/master
Run Code Online (Sandbox Code Playgroud)
这只会默认从原点获取master.有关详细信息,请参阅:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
编辑:刚刚意识到这与-t选项对git remote add的作用相同.如果您不想删除遥控器并使用-t再次添加遥控器,那么在添加遥控器后,至少这是一种很好的方法.
为了完整起见,这是一个新结账的示例命令:
git clone --branch gh-pages --single-branch git://github.com/user/repo
Run Code Online (Sandbox Code Playgroud)
正如其他答案所述,它设置remote.origin.fetch如下:
[remote "origin"]
url = git://github.com/user/repo
fetch = +refs/heads/gh-pages:refs/remotes/origin/gh-pages
Run Code Online (Sandbox Code Playgroud)
小智 6
我的解决方法:
git fetch --depth=1
git checkout <branch_name>
Run Code Online (Sandbox Code Playgroud)
如果您没有本地克隆:
git clone --depth 1 -b <branch_name> <repo_url>
Run Code Online (Sandbox Code Playgroud)
小智 5
git版本:2.74
我是这样做的:
git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]
Run Code Online (Sandbox Code Playgroud)
git 版本 2.16.1.windows.4
只需执行git fetch remoteRepositoryName branchName (eg: git fetch origin my_local_branch)就足够了。将完成提取,并将创建一个具有相同名称的新本地分支,并将跟踪设置为远程分支。
然后执行git checkout branchName
最简单的方法来做到这一点
git fetch origin <branch> && git checkout <branch>
Run Code Online (Sandbox Code Playgroud)
示例:我想从 origin 获取 uat 分支并切换到 this 作为当前工作分支。
git fetch origin uat && git checkout uat
Run Code Online (Sandbox Code Playgroud)
这种方法对我有用。
获取目标分支的远程分支:
git fetch origin branch-name
Run Code Online (Sandbox Code Playgroud)
查看目标分支:
git checkout -b branch-name origin/branch-name
Run Code Online (Sandbox Code Playgroud)
在这里,我尝试成功获取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'
Run Code Online (Sandbox Code Playgroud)