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


当前回答

git 版本 2.16.1.Windows.4

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

然后执行git checkout branchName

其他回答

我的解决方法:

git fetch --depth=1
git checkout <branch_name>

如果您没有本地克隆:

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

一种方法是:

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

但这并没有设置跟踪。

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

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

git fetch <remote_name> <branch_name>

为我工作。

一种方法是:

在.git/config中获取远程回购应该设置为获取任何分支:

   [remote "origin"]
            fetch = +refs/heads/*:refs/remotes/origin/*

获取远程分支:

git fetch origin branch-name

创建一个本地分支'branch-name',用于从原点跟踪远程分支'branch-name'。

git checkout -b branch-name origin/branch-name

列出所有分支

git branch -a

让我用我的两便士来曲解马德森先生的回答:

Git获取<remote_name_or_url> <branch_name> git checkout FETCH_HEAD -B <branch_name>

与马德森的提议相比,这两条线路的主要优势在于,即使分支机构已经在本地存在,它也能工作。