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


当前回答

一种方法是:

git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>

但这并没有设置跟踪。

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

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

其他回答

选择任何您想要使用的<remote_name>(可以随意使用origin 跳过第一步。) Git远程添加<remote_name> <remote_url> Git获取<remote_name> <分支> 选择任何您想使用的<your_local_branch_name>。可以与<branch>相同。 Git checkout <remote_name>/<branch> -b <your_local_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 fetch --depth=1
git checkout <branch_name>

如果您没有本地克隆:

git clone --depth 1 -b <branch_name> <repo_url>

git 版本 2.16.1.Windows.4

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

然后执行git checkout branchName

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

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