kjo
kjo

Reputation: 35331

Trying to make sense of git fetch

Suppose that the repo at $REPO_URL has multiple branches, including one called dev. Now, suppose I run the following commands, one right after the other

% 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

What must I do at this point so that for each branch of the form remotes/origin/X there's a local branch X that matches it exactly? By this I mean that the following two commands would produce identical output:

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

and also that the output of git branch -a would end up looking like this:

* 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

Upvotes: 3

Views: 83

Answers (2)

Carl Norum
Carl Norum

Reputation: 225202

A shell for loop will do it:

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

Upvotes: 2

PinnyM
PinnyM

Reputation: 35531

You don't need fetch for this, because you already did that when you cloned the repo. What you want is a local branch to match each remote tracking branch, and for this you can use checkout:

git checkout -b issue6 origin/issue6

So you can have a script that loops through all remote branches and checks them out.

As mentioned in comments, recent versions of git can use a simpler command:

git checkout issue6

Upvotes: 3

Related Questions