试图理解git fetch

时间:2013-03-07 19:44:39

标签: git

假设$REPO_URL处的回购拥有多个分支,包括一个名为dev的分支。现在,假设我运行以下命令,一个接着另一个

% git clone $REPO_URL --branch dev wd
% cd wd
% git branch -a
* dev
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/issue6
  remotes/origin/issue26
  remotes/origin/issue30
  ...
  remotes/origin/issue709
  remotes/origin/issue712
  remotes/origin/issue716
  remotes/origin/master

此时我必须做什么,以便对于remotes/origin/X形式的每个分支,有一个与其完全匹配的本地分支X?这意味着以下两个命令将产生相同的输出:

% git rev-parse remotes/origin/X
% git rev-parse X

并且git branch -a的输出最终看起来像这样:

* dev
  issue6
  issue26
  issue30
  ...
  issue709
  issue712
  issue716
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/issue6
  remotes/origin/issue26
  remotes/origin/issue30
  ...
  remotes/origin/issue709
  remotes/origin/issue712
  remotes/origin/issue716
  remotes/origin/master

2 个答案:

答案 0 :(得分:3)

您不需要fetch,因为您在克隆回购时已经这样做了。你想要的是一个匹配每个远程跟踪分支的本地分支,为此你可以使用checkout

git checkout -b issue6 origin/issue6

因此,您可以拥有一个循环遍历所有远程分支并将其签出的脚本。

正如评论中所提到的,最新版本的git可以使用更简单的命令:

git checkout issue6

答案 1 :(得分:2)

shell for循环将执行此操作:

for BRANCH in $(git branch -r | grep -v HEAD) # list of remote branches (excluding HEAD)
do
    git checkout --track ${BRANCH}
done