How to fetch all Git branches

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to fetch all Git branches



I cloned a Git repository, which contains about five branches. However, when I do git branch I only see one of them:


git branch


$ git branch
* master



I know that I can do git branch -a to see all the branches, but how would I pull all the branches locally so when I do git branch, it shows the following?


git branch -a


git branch


$ git branch
* master
* staging
* etc...





Also discussed at stackoverflow.com/questions/67699/…
– gliptak
Sep 25 '12 at 14:40





This question shows how to get all branches after using the --single-branch setting when cloning: stackoverflow.com/questions/17714159/… (git fetch --all will never work if you've specified only one branch!)
– Matthew Wilcoxson
May 21 '15 at 17:10


--single-branch


git fetch --all





You will not see that output ever because the asterisk represents the branch that is currently checkout out. Since you can only have one branch checked out at once, you can have only one asterisk on the left of your branch listing.
– Robino
Dec 7 '15 at 15:09





The top-ranked answer below misses the OP's intent. I recommend that you look at stackoverflow.com/a/72156/342839 instead. git checkout -b <branch> seems like the most likely answer.
– Reece
May 25 '16 at 20:53



git checkout -b <branch>





I saw a lot of answers but none of them mentioned what I think is probably the easiest way to do what you want: git clone --bare <repo url> .git (notice you need to add "--bare" and ".git" at the end to clone the repo as a "bare" repo), then git config --bool core.bare false (sets the "bare" flag to false), then git reset --hard (moves the HEAD to current HEAD on the repo). Now if you git branch you should see all branches from the repo you cloned.
– Gabriel Ferraz
Mar 25 '17 at 17:37



git clone --bare <repo url> .git


git config --bool core.bare false


git reset --hard


git branch




22 Answers
22



You can fetch one branch from all remotes like this:


git fetch --all



fetch updates local copies of remote branches so this is always safe for your local branches BUT:


fetch



fetch will not update local branches (which track remote branches); If you want to update your local branches you still need to pull every branch.


fetch



fetch will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches:
git branch -a


fetch


git branch -a



To update local branches which track remote branches:


git pull --all



However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all:


git pull --all


git branch -r | grep -v '->' | while read remote; do git branch --track "$remote#origin/" "$remote"; done


git branch -r | grep -v '->' | while read remote; do git branch --track "$remote#origin/" "$remote"; done
git fetch --all
git pull --all



(it seems that pull fetches all branches from all remotes, but I always fetch first just to be sure)



Run the first command only if there are remote branches on the server that aren't tracked by your local branches.



P.S. AFAIK git fetch --all and git remote update are equivalent.


git fetch --all


git remote update



Kamil Szot's comment, which 74 (at least) people found useful.



I had to use:


for remote in `git branch -r`; do git branch --track $remote#origin/ $remote; done



because your code created local branches named origin/branchname and
I was getting "refname 'origin/branchname' is ambiguous whenever I
referred to it.


origin/branchname





Sorry. I can't imagine that this is what the OP actually wants. The 'pull' command is 'fetch+merge' and the merge part will overlay all the branches on top of one another - leaving one giant mess.
– GoZoner
Apr 25 '12 at 14:06





that fetch wouldn't create a new remote branch you still need to check it out with git checkout -b localname remotename/remotebranch
– Learath2
Oct 20 '12 at 17:06


git checkout -b localname remotename/remotebranch





I had to use for remote in `git branch -r`; do git branch --track $remote#origin/ $remote; done because your code created local branches named origin/branchname and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.
– Kamil Szot
Sep 22 '13 at 23:46



for remote in `git branch -r`; do git branch --track $remote#origin/ $remote; done





I don't know if I'm using a different version of GIT, but I had to amend the script to git pull --all; for remote in `git branch -r | grep -v >`; do git branch --track $remote#origin/ $remote; done. The change strips out HEAD.
– kim3er
Sep 29 '13 at 13:16



git pull --all; for remote in `git branch -r | grep -v >`; do git branch --track $remote#origin/ $remote; done





For the Windows folks: for /F %remote in ('git branch -r') do ( git branch --track %remote) && git fetch --all && git pull --all
– Max
Dec 20 '13 at 6:03



for /F %remote in ('git branch -r') do ( git branch --track %remote) && git fetch --all && git pull --all



To list remote branches:
git branch -r


git branch -r



You can check them out as local branches with:
git checkout -b LocalName origin/remotebranchname


git checkout -b LocalName origin/remotebranchname





This is exactly what I was looking for when I found the question above. I suspect many people looking for how to pull a remote branch definitely don't want to merge the branch into their current working copy, but they do want a local branch identical to the remote one.
– Frug
Oct 19 '12 at 16:03





Even if the branch is not visible locally, I can do git checkout remotebranchnameand it works. what's the difference with your solution?
– François Romain
Nov 6 '14 at 10:43


git checkout remotebranchname





Its default behaviour now. Wasn't the case on older git versions. Using git checkout remotebranchname used to just create a new unrelated branch that is named remotebranchname.
– Learath2
Nov 15 '14 at 13:10


git checkout remotebranchname





The accepted answer does something fundamentaly different and to be frank I don't even understand why its the accepted answer
– Learath2
Jul 23 '15 at 16:28





The OP asked for all branches. This answer only does one.
– Ted Bigham
Aug 5 '16 at 15:19



You will need to create local branches tracking remote branches.



Assuming that you've got only one remote called origin, this snippet will create local branches for all remote tracking ones:


origin


for b in `git branch -r | grep -v -- '->'`; do git branch --track $b##origin/ $b; done



After that, git fetch --all will update all local copies of remote branches.


git fetch --all



Also, git pull --all will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.


git pull --all





This robustifies the solution against branch names containing shell metacharacters (as per pinkeen's comment on the other answer), and avoids spurious error output: git branch -r | grep -v -- ' -> ' | while read remote; do git branch --track "$remote#origin/" "$remote" 2>&1 | grep -v ' already exists'; done
– Daira Hopwood
Aug 11 '15 at 23:28






Are you sure that git pull --all will update all local tracking branches? As far as I can tell it only updates the current branch from all remotes.
– Andy
Jul 26 '16 at 16:07


git pull --all





Did this. Local branches matching remote branches were not created. What is the git command that simply says "pull all remote branches creating local ones if they do not exist?"
– JosephK
Feb 11 '17 at 10:47





@JosephK perhaps your remote is not called origin? See this answer which will work on all remote names.
– Tom Hale
Sep 14 at 15:53


origin



If you do:


git fetch origin



then they will be all there locally. If you then perform:


git branch -a



you'll see them listed as remotes/origin/branch-name. Since they are there locally you can do whatever you please with them. For example:


git diff origin/branch-name



or


git merge origin/branch-name



or


git checkout -b some-branch origin/branch-name





Just found this page on google... this was the actual type of answer I was seeking. I tried the first command but received an error: [$ git fetch --all origin fatal: fetch --all does not take a repository argument] --- Using "git fetch --all" seems to do the trick. Thanks for the lead!
– longda
Mar 29 '13 at 18:26






Fixed (eliminated --all)
– GoZoner
Mar 29 '13 at 19:47


--all





git fetch -all fetches all branches of all remotes. git fetch origin fetches all branches of the remote origin. The later is what the OP was asking.
– GoZoner
Apr 24 '13 at 19:37


git fetch -all


git fetch origin


origin





--all means "all remotes", not "all branches of a given remote". The latter is implied by any fetch from a remote.
– spacediver
Jun 3 '13 at 14:58


--all





This is not the way to pull all branches into local repo, from remote repo.
– Vladimir Despotovic
Nov 20 '16 at 23:41


$ git remote update
$ git pull --all



This assumes all branches are tracked.



If they aren't you can fire this in Bash:


for remote in `git branch -r `; do git branch --track $remote; done



Then run the command.





When I try that I still get the same result as above.
– David542
Apr 25 '12 at 9:10






last one worked for me.. with a few git errors
– Jacob Lowe
Jan 5 '13 at 5:52





Same as @JacobLowe, I got the error, but it worked anyway; 'fatal: A branch named 'origin/master' already exists.'
– AnneTheAgile
Jul 21 '14 at 20:15





This is ugly as it will try to create a branch for -> which will likely exist in the output of git branch -r as ` origin/HEAD -> origin/master`
– Tom Hale
Sep 14 at 15:57


->


git branch -r





Moreover, it doesn't work. I get output: Branch 'origin/quote-filenames' set up to track local branch 'master'. The desired output is: Branch 'quote-filenames' set up to track remote branch 'quote-filenames' from 'origin'. This is backwards, setting the origin to track the remote. See this answer for a fix.
– Tom Hale
Sep 14 at 16:00



Branch 'origin/quote-filenames' set up to track local branch 'master'.


Branch 'quote-filenames' set up to track remote branch 'quote-filenames' from 'origin'.



Use git fetch && git checkout RemoteBranchName.


git fetch && git checkout RemoteBranchName



It works very well for me...





This is the new best answer, y'all. I don't know if maybe this wasn't possible before, but recent versions of Git at least will notice that you are trying to checkout a remote branch and will automatically set up the local tracking branch for you (and you don't need to specify origin/branch; it suffices to say only branch).
– Neil Traft
Jun 27 '14 at 18:12



origin/branch


branch





Exactly what i wanted! Something simple!
– maloo
Apr 21 '16 at 12:53





This does not answer the original question: "How would I pull all the branches locally?" It is pulling them one-by-one, which isn't scalable.
– ingyhere
Jan 1 '17 at 17:39





This was the only answer which allowed me to pull remote branches in every situation I've encountered
– Emmanuel Buckshi
Apr 19 '17 at 22:07



The bash for loop wasn't working for me, but this did exactly what I wanted. All the branches from my origin mirrored as the same name locally.


git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'



Edited: See Mike DuPont's comment below. I think I was trying to do this on a Jenkins Server which leaves it in detached head mode.





That produces fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository after a simple clone. Have to detach head first. I did this with git checkout <SHA>
– brannerchinese
Apr 15 '15 at 17:28



fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository


git checkout <SHA>





My Solution using this is git checkout --detach # detach the head and then git fetch origin '+refs/heads/*:refs/heads/*
– Mike DuPont
Jun 29 '15 at 15:09



git checkout --detach # detach the head


git fetch origin '+refs/heads/*:refs/heads/*





This one worked for me, except I also use the --tags parameter. I wish there was a standard, simple front end for git, the number of simple things in git that need 10-plus stack overflow answers is ridiculous!
– kristianp
Feb 18 '16 at 23:13






@kristianp Have you checked out Ungit or GitKraken?
– dragon788
Apr 27 '16 at 20:33





@dragon788 I've been using SourceTree for a git GUI, but I was really talking about a simpler command-line for scripting tasks.
– kristianp
Apr 29 '16 at 4:28



When you clone a repository all the information of the branches is actually downloaded but the branches are hidden. With the command


$ git branch -a



you can show all the branches of the repository, and with the command


$ git checkout -b branchname origin/branchname



you can then "download" them manually one at a time.



However, there is a much cleaner and quicker way, though it's a bit complicated. You need three steps to accomplish this:



First step



create a new empty folder on your machine and clone a mirror copy of the .git folder from the repository:


$ cd ~/Desktop && mkdir my_repo_folder && cd my_repo_folder
$ git clone --mirror https://github.com/planetoftheweb/responsivebootstrap.git .git



the local repository inside the folder my_repo_folder is still empty, there is just a hidden .git folder now that you can see with a "ls -alt" command from the terminal.



Second step



switch this repository from an empty (bare) repository to a regular repository by switching the boolean value "bare" of the git configurations to false:


$ git config --bool core.bare false



Third Step



Grab everything that inside the current folder and create all the branches on the local machine, therefore making this a normal repo.


$ git reset --hard



So now you can just type the command git branch and you can see that all the branches are downloaded.


git branch



This is the quick way in which you can clone a git repository with all the branches at once, but it's not something you wanna do for every single project in this way.





Upvoted for mention of hidden branches. helped me understand the local branch tracking immensely.
– mburke05
May 25 '17 at 17:03





Its a good answer, but the question implies something for every day use. Its not practical to clone the repository everytime.
– Z. Khullah
Mar 1 at 4:19





Why the reset creates all the branches locally?
– Z. Khullah
Mar 1 at 4:20



I usually use nothing else but commands like this:


git fetch origin
git checkout --track origin/remote-branch



A little shorter version:


git fetch origin
git checkout -t origin/remote-branch





git fetch origin before checking out did the trick for me.
– Suraj
Mar 25 '15 at 6:30



You can fetch all the branches by:


git fetch --all



or:


git fetch origin --depth=10000 $(git ls-remote -h -t origin)



The --depth=10000 parameter may help if you've shallowed repository.


--depth=10000



To pull all the branches, use:


git pull --all



If above won't work, then precede the above command with:


git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'



as the remote.origin.fetch could support only a specific branch while fetching, especially when you cloned your repo with --single-branch. Check this by: git config remote.origin.fetch.


remote.origin.fetch


--single-branch


git config remote.origin.fetch



After that you should be able to checkout any branch.



See also:



To push all the branches to the remote, use:


git push --all



eventually --mirror to mirror all refs.


--mirror



If your goal is to duplicate a repository, see: Duplicating a repository article at GitHub.





Awesome...I tried everything else before your solution in this page. Thanks a lot!
– Sujoy
Mar 7 '17 at 23:45





I used shallow cloning (depth=1) and the config also specified one specific branch for fetch - the depth=1000 parameter was the fix that helped me to checkout a specific remote branch
– Sandra
Nov 21 '17 at 9:47


depth=1


fetch


depth=1000





pull --all doesn't pull all the branches, but all the remotes
– Z. Khullah
Mar 1 at 4:22


pull --all





Nice trick with the config, though! Would fetch them all, all the time
– Z. Khullah
Mar 1 at 4:23



If you are here seeking a solution to get all branches and then migrate everything to another Git server, I put together the below process. If you just want to get all the branches updated locally, stop at the first empty line.


git clone <ORIGINAL_ORIGIN>
git branch -r | awk -F'origin/' '!/HEAD|master/print $2 " " $1"origin/"$2' | xargs -L 1 git branch -f --track
git fetch --all --prune --tags
git pull --all

git remote set-url origin <NEW_ORIGIN>
git pull
<resolve_any_merge_conflicts>
git push --all
git push --tags
<check_NEW_ORIGIN_to_ensure_it_matches_ORIGINAL_ORIGIN>





Very helpful; exactly what I needed. The only thing I had to change was in the second line, added single quotes around 'HEAD' and 'master'; probably because I'm using zsh. Thanks!
– sockmonk
Apr 15 '14 at 16:10





This is basically doing the following: (1) Obtaining the actual names of remote branches [not head, not master]; (2) Completely telling Git to track them [not all solutions do this]; (3) Fetching and pulling everything from these branches [including tags]; (4) Setting a new origin and walking through pushing absolutely everything up. Again, most of the other solutions fail to move all pieces. This does it all.
– ingyhere
Nov 23 '15 at 23:37





I removed the antipattern of running grep then awk and condensed the grep commands into the awk command. Thanks tripleee!
– ingyhere
Jan 31 '17 at 20:29






Read this and never write git fetch git pull stackoverflow.com/a/292359/1114926
– Green
Jun 20 at 6:32


git fetch git pull





Git pull does indeed do a fetch first but it's easier to tell if the problem is from the fetch part of pull or the subsequent merge part of pull when fetch is executed independently.
– ingyhere
Aug 7 at 23:00



pull


fetch


fetch


pull


merge


pull


fetch



I believe you have clone the repository by


git clone https://github.com/pathOfrepository



now go to that folder using cd


cd pathOfrepository



if you type git status


git status



you can see all


On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean



to see all hidden branch type


git branch -a



it will list all the remote branch



now if you want to checkout on any particular branch just type


git checkout -b localBranchName origin/RemteBranchName



After you clone the master repository, you just can execute


git fetch && git checkout <branchname>





simple. and worked to get a branch from remote origin
– sirvon
Sep 21 '15 at 4:26





Thanks a lot, this should be the correct answer.
– Reza
Sep 18 '16 at 9:17





This does not answer the original question: "How would I pull all the branches locally?" It is pulling them one-by-one, which isn't scalable. Consider the case of 100 branches.
– ingyhere
Jan 1 '17 at 17:41




Make sure all the remote branches are fetchable in .git/config file.


.git/config



In this example, only the origin/production branch is fetchable, even if you try to do git fetch --all nothing will happen but fetching the production branch:


origin/production


git fetch --all


production


[origin]
fetch = +refs/heads/production:refs/remotes/origin/production



This line should be replaced by:


[origin]
fetch = +refs/heads/*:refs/remotes/origin/*



Then run git fetch etc...


git fetch





To inspect: git config --get remote.origin.fetch and then to (destructively) set it: git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
– qneill
Sep 21 '17 at 22:00



git config --get remote.origin.fetch


git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'





or modify config text file in .git directory, worked for me
– FDIM
Oct 26 '17 at 9:13



Looping didn't seem to work for me and I wanted to ignore origin/master.
Here's what worked for me.


git branch -r | grep -v HEAD | awk -F'/' 'print $2 " " $1"/"$2' | xargs -L 1 git branch -f --track



After that:


git fetch --all
git pull --all





A variation of this is the correct answer, but this one does not work in all edge cases. Also, the branch names can be funky. So I did the following: git branch -r | grep -v HEAD | grep –v master | awk -F'origin/' 'print $2 " " $1"origin/"$2' | xargs -L 1 git branch -f --track ; git fetch --all ; git pull --all ; AND THAT DID THE TRICK!
– ingyhere
Feb 5 '14 at 23:49





A stylistic improvement to avoid the grep | awk antipattern: git branch -r | awk -F 'origin/' '!/HEAD|master/{ ...
– tripleee
Nov 13 '15 at 11:14


grep | awk


git branch -r | awk -F 'origin/' '!/HEAD|master/{



Just those 3 commands will get all the branches


git clone --mirror repo.git .git (gets just .git - bare repository)

git config --bool core.bare false

git reset --hard



I wrote a little script to manage cloning a new repo and making local branches for all the remote branches.



You can find the latest version here:


#!/bin/bash

# Clones as usual but creates local tracking branches for all remote branches.
# To use, copy this file into the same directory your git binaries are (git, git-flow, git-subtree, etc)

clone_output=$((git clone "$@" ) 2>&1)
retval=$?
echo $clone_output
if [[ $retval != 0 ]] ; then
exit 1
fi
pushd $(echo $clone_output | head -1 | sed 's/Cloning into .(.*)..../1/') > /dev/null 2>&1
this_branch=$(git branch | sed 's/^..//')
for i in $(git branch -r | grep -v HEAD); do
branch=$(echo $i | perl -pe 's/^.*?///')
# this doesn't have to be done for each branch, but that's how I did it.
remote=$(echo $i | sed 's//.*//')
if [[ "$this_branch" != "$branch" ]]; then
git branch -t $branch $remote/$branch
fi
done
popd > /dev/null 2>&1



To use it, just copy it into your git bin directory (for me, that’s C:Program Files (x86)Gitbingit-cloneall), then, on the command line:


C:Program Files (x86)Gitbingit-cloneall


git cloneall [standard-clone-options] <url>



It clones as usual, but creates local tracking branches for all remote branches.



For Windows users using PowerShell:


git branch -r | ForEach-Object
# Skip default branch, this script assumes
# you already checked-out that branch when cloned the repo
if (-not ($_ -match " -> "))
$localBranch = ($_ -replace "^.*/", "")
$remoteBranch = $_.Trim()
git branch --track "$localBranch" "$remoteBranch"


git fetch --all
git pull --all





The command will ignore the branch which name with "/"
– John_J
Dec 29 '17 at 10:01



We can put all branch or tag names in a temporary file, then do git pull for each name/tag:


git branch -r | grep origin | grep -v HEAD| awk -F/ 'print $NF' > /tmp/all.txt
git tag -l >> /tmp/all.txt
for tag_or_branch in `cat /tmp/all.txt`; do git checkout $tag_or_branch; git pull origin $tag_or_branch; done



Based on the answer by Learath2, here's what I did after doing git clone [...] and cd-ing into the created directory:


git clone [...]


cd



git branch -r | grep -v master | awk print$1 | sed 's/^origin/(.*)$/1 &/' | xargs -n2 git checkout -b


git branch -r | grep -v master | awk print$1 | sed 's/^origin/(.*)$/1 &/' | xargs -n2 git checkout -b



Worked for me but I can't know it'll work for you. Be careful.


git remote add origin https://yourBitbucketLink

git fetch origin

git checkout -b yourNewLocalBranchName origin/requiredRemoteBranch (use tab :D)



Now locally your yourNewLocalBranchName is your requiredRemoteBranch.


yourNewLocalBranchName


requiredRemoteBranch



Here's something I'd consider robust:


HEAD


origin/HEAD


origin


for b in $(git branch -r --format='%(refname:short)'); do
[[ "$b#*/" = HEAD ]] && continue
git show-ref -q --heads "$b#*/" || git branch --track "$b#*/" "$b";
done
git pull --all



It's not necessary to git fetch --all as passing -all to git pull passes this option to the internal fetch.


git fetch --all


-all


git pull


fetch



Credit to this answer.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard