获取并签出没有分离头的远程分支

时间:2014-01-16 17:17:08

标签: git

我通过git fetch origin name-of-branch:refs/remotes/name-of-branch获取远程分支。

我使用git checkout name-of-branch我处于一个独立的头状态。我现在可以使用git checkout -b some-branch-name到达命名分支。有没有办法在一个命令中将远程分支签出到命名分支(甚至同名)?

如果我使用git checkout -b name-of-remote-branch(即使用相同的名称),那么请检查另一个分支,然后再次检查该分支git checkout name-of-remote-branch,我得到

warning: refname 'name-of-remote-branch' is ambiguous.

但是,如果我首先使用git pull然后使用checkout:

,则不会发生这种情况
Branch name-of-pulled-branch set up to track remote branch name-of-pulled-branch from origin

我认为,对于获取的分支而言,我遇到的很多麻烦都与设置为跟踪远程分支的事实有关。为什么会这样,有没有办法检查取出的分支并将其设置为跟踪远程分支,就像我使用git pull一样?

1 个答案:

答案 0 :(得分:2)

你想:

git fetch origin name-of-branch:refs/remotes/name-of-branch
git checkout -b some-branch-name name-of-remote-branch --track

这将设置跟踪,以便git pull / push将Just Work。