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


当前回答

这是最简单的方法

  git fetch origin <branch> && git checkout <branch>

示例:我想从原点获取uat分支,并切换到此作为当前工作分支。

   git fetch origin uat && git checkout uat

其他回答

我的解决方法:

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

如果您没有本地克隆:

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

如果你想改变默认的“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/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>

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

答案取决于你想要完成什么。

If it is a one time deal from a different repository and you don't need a reference (e.g. to merge GitHub Pull Requests, where <remote_url> is like https://github.com/USER/REPO.git), then you can use: git checkout -b <local_branch> <local_branch_to merge_into> git pull <remote_url> <remote_branch> If you want to update and track the branch you have to set first the remote and there are 4 alternatives: If you are cloning a new repository (e.g. working only on it) git clone --single-branch --branch remote_branch remote_url If you are adding a new remote to your working directory # multiple -t options are allowed git remote add -t <remote_branch> <remote_repo> <remote_url> If you are adding the branch restriction to an existing remote in your working directory # with --add it will add the branch instead of setting it # you can add multiple branches with multiple --add lines # wildcards are allowed, # e.g. branch_v\* matching branch_v1, branch_v2, ... git remote set-branches [--add] <remote_repo> <remote_branch> You could also skip the restrictions because clone by default fetches only the main branch and remote add does not fetch branches. Buth then you'll have to mention the remote branch all the remote branch all the time you fetch the remote_repo. git remote add <remote_repo> <remote_url> After setting the remote you can fetch the remote branch, checkout and pull: # If you set only one <remote_branch> in the restrictions above (i.e no option 4), # then you can omit it and still only <remote_branch> will be fetched git fetch <remote_repo> [<remote_branch>] # without -b the local branch name is guessed to be the same as the remote one git checkout --track [-b <local_branch>] <remote_repo>/<remote_branch>

检查远程和已经或将要获取的分支的最佳命令是git remote show <remote_repo>。它会打印“Remote branch:”下的分支列表,还会告诉你它们是否被获取,以及是否被跟踪。

你也可以通过使用git branch -r列出已知的远程分支来检查远程分支的限制,如果你有很多远程,可以结合grep,或者通过检查git配置文件.git/config中的远程详细信息。它将包含如下部分:

[remote "<remote_repo>"]
    url = <remote_url>
    fetch = +refs/heads/<remote_branch>:refs/remotes/<remote_repo>/<remote_branch>

编辑配置文件将改变限制,但我同意@alexk的意见,这不是一个好主意。

NOTE: If a branch is not in the list of branches of a remote (visible in git remote show or the config file), then you will not be able to have a reference to it, git will save it to the temporary FETCH_HEAD and you will not be able to track it or to use it directly in git checkout. This is the problem that brought me to this thread (the opposite of the one in the question): I cloned a repo with GitHub client gh repo clone USER/REPO and it added automatically "upstream", the repository forked from, restricted only to the branch "master". I was unable to checkout other branches and getting errors like "fatal: '<remote_repo>/<remote_branch>' is not a commit and a branch '<local_branch>' cannot be created from it". I fixed it with: git remote set-branches <remote_repo> \*.