我想获取远程存储库的单个分支(不是所有分支),并创建一个本地跟踪分支,该分支可以跟踪对该远程分支的进一步更新。远程存储库中的其他分支非常大,所以我希望避免获取它们。我怎么做呢?


当前回答

为了完整起见,这里有一个新签出的示例命令:

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 fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>

但这并没有设置跟踪。

要了解更多信息,请参阅git获取的文档。

编辑:正如@user1338062所指出的:如果你想添加新分支的地方还没有存储库的本地克隆,但你想创建一个新的本地存储库,那么git clone——branch <branch_name>——single-branch <repo_url>提供了一个更短的解决方案。

为了完整起见,这里有一个新签出的示例命令:

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 版本 2.16.1.Windows.4

只是做一个git获取remoteRepositoryName branchName(例如:git获取origin my_local_branch)就足够了。Fetch将完成,并将创建一个具有相同名称的新本地分支,并将跟踪设置为远程分支。

然后执行git checkout branchName

答案实际上取决于当前的跟踪分支列表。你可以使用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 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'