I have a master
and a development
branch, both pushed to GitHub. I\'ve clone
d, pull
ed, and fetch
ed, but I re
OK, when you clone your repo, you have all branches there...
If you just do git branch
, they are kind of hidden...
So if you'd like to see all branches name, just simply add --all
flag like this:
git branch --all
or git branch -a
If you just checkout to the branch, you get all you need.
But how about if the branch created by someone else after you clone?
In this case, just do:
git fetch
and check all branches again...
If you like to fetch and checkout at the same time, you can do:
git fetch && git checkout your_branch_name
Also created the image below for you to simplify what I said:
The accepted answer of git branch -a
only shows the remote branches. If you attempt to checkout
the branches you'll be unable to unless you still have network access to the origin server.
If you’re looking for a self-contained clone or backup that includes all remote branches and commit logs, use:
git clone http://user@repo.url
git pull --all
Credit: Gabe Kopley's for suggesting using git pull --all
.
Note:
Of course, if you no longer have network access to the remote/origin
server, remote/origin branches
will not have any updates reflected in them. Their revisions will reflect commits from the date and time you performed the 2 commands above.
To checkout ALL your clone branches to local branches with one command, use one of the bash commands below:
$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done
OR
If your repo has nested branches then this command will take that into account:
for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done
A git clone
is supposed to copy the entire repository. Try cloning it, and then run git branch -a
. It should list all the branches. If then you want to switch to branch "foo" instead of "master", use git checkout foo
.
For copy-paste into command line:
git checkout master ; remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch -D $brname ; git checkout -b $brname $remote/$brname ; done ; git checkout master
For more readibility:
git checkout master ;
remote=origin ;
for brname in `
git branch -r | grep $remote | grep -v master | grep -v HEAD
| awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do
git branch -D $brname ;
git checkout -b $brname $remote/$brname ;
done ;
git checkout master
Based on answer of VonC.
Here is another short one-liner command which creates local branches for all remote branches:
(git branch -r | sed -n '/->/!s#^ origin/##p' && echo master) | xargs -L1 git checkout
It works also properly if tracking local branches are already created.
You can call it after the first git clone
or any time later.
If you do not need to have master
branch checked out after cloning, use
git branch -r | sed -n '/->/!s#^ origin/##p'| xargs -L1 git checkout
This isn't too much complicated, very simple and straight forward steps are as follows;
git fetch origin
This will bring all the remote branches to your local.
git branch -a
This will show you all the remote branches.
git checkout --track origin/<branch you want to checkout>
Verify whether you are in the desired branch by the following command;
git branch
The output will like this;
*your current branch
some branch2
some branch3
Notice the * sign that denotes the current branch.