Skip to content

Instantly share code, notes, and snippets.

@grimzy
Created September 15, 2017 02:15
Show Gist options
  • Save grimzy/a1d3aae40412634df29cf86bb74a6f72 to your computer and use it in GitHub Desktop.
Save grimzy/a1d3aae40412634df29cf86bb74a6f72 to your computer and use it in GitHub Desktop.
Git pull all remote branches
#!/usr/bin/env bash
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
@Timmmm
Copy link

Timmmm commented Aug 22, 2018

--all just means to fetch from all remotes.

@gwierink
Copy link

After cloning a repo and wanting to fetch all branches, git complains (rightfully so) that it does not think it makes sense to create 'HEAD' manually. When I threw out master, all was well:
git branch -r | grep -v '\->' | grep -v 'master' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

@rhujisawa
Copy link

GIT_SSH_COMMAND='ssh -i ~/.ssh/ key' git branch -r | grep -v '\->' | grep -v 'master' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

@Rocking80
Copy link

when I executed git branch -r | grep -v '\->', I got lots of previous branches which not used yet. why?

@ankurash
Copy link

pull --all and fetch --all do not pull all branches, just all the remotes. Have tested it multiple times now.
I haven't found a solution and I'm desperately looking for a real solution.
Meanwhile, have been using the following workaround (again, this is not a solution).
for remote in `git branch -r | grep -v '\->'`; do (git branch --track ${remote#origin/} $remote; git checkout ${remote#origin/}; git pull ); done; git checkout master; git pull --all
This tracks and pulls all branches but has a ridiculous overhead of changing the repo contents when checking out in every branch.

@ElfSundae
Copy link

If there are existing branch names, e.g. master, then set -e option will cause this command fails and some branches may not be checkout.
This issue can be fixed by appending || true for git branch --track command:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote" || true; done 2>/dev/null

@alanmarcos
Copy link

If there are existing branch names, e.g. master, then set -e option will cause this command fails and some branches may not be checkout.
This issue can be fixed by appending || true for git branch --track command:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote" || true; done 2>/dev/null

thank you this helped me big time!

@ElfSundae
Copy link

You're welcome. I use below now:

remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch --track $brname $remote/$brname || true; done 2>/dev/null

From https://gist.github.com/ElfSundae/92a5868f418ec3187dfff90fe6b20387

@will-tam
Copy link

Hello and thanks everyone for this tips

@maskym
Copy link

maskym commented Jul 21, 2020

Thank you so much ElfSundae, I've looked for this answer so much, only to find fetch --all ...

@HoffiMuc
Copy link

HoffiMuc commented Nov 25, 2020

current=$(git branch --show-current) ; for brname in $(git branch -r | grep origin | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+//,"",$1); print $1}'); do echo git checkout $brname ; git checkout $brname ; echo git pull ; git pull ; done ; echo git checkout $current ;git checkout $current

@andreasslc
Copy link

andreasslc commented Apr 24, 2021

for abranch in $(git branch -a | grep -v HEAD | grep remotes | sed "s/remotes\/origin\///g"); do git checkout $abranch ; done
This statement will checkout all branches when executed in local repo location. I use it frequently.

@leoplaw
Copy link

leoplaw commented May 21, 2021

Thank you for this! It was very helpful.

@skmn1
Copy link

skmn1 commented Oct 5, 2021

thank you

@m3asmi
Copy link

m3asmi commented Mar 14, 2022

I use this:
git branch -r | grep -v '\->' | sed -e 's/^origin\///' | while read remote; do echo "parsing branch $remote"; branch=${remote/origin\//}; git checkout "$branch"; git reset --hard $remote ; git pull; echo "$remote done";done

@isho777
Copy link

isho777 commented Apr 12, 2022

I think git is still being developed. Its like another programming language on its own. I'd like all GUI usage with drag and drop in the future

@Wandalen
Copy link

Wandalen commented Jun 1, 2022

Hi,
According to the doc on pull, the --all option only affects the fetch part of the pull command.
So isn't it kind of useless to do a fetch --all before a pull --all ?
Also I have doubts that git pull --all does indeed pull all remote branch and not just the current one.
What do you think ?

Confirmed. That is useless. Any working alternative?

@jcwren
Copy link

jcwren commented Jun 29, 2022

git branch -r | grep -v '->' | tr -d 'origin/' | while read remote; do echo "parsing branch $remote"; git checkout "$remote"; git reset --hard $remote ; git pull; echo "$remote done";done

Your tr command is incorrect, as it deletes characters in the list. You want sed.

$ echo "origin/docubranch" | tr -d 'origin/'
emtesdcubach
$ echo "origin/docubranch" | sed -e 's/^origin\///'
docubranch

@m3asmi
Copy link

m3asmi commented Jul 13, 2022

@jcwren thanks, I fixed it

@CynCity17
Copy link

This was so helpful! Thank you!

@haiderGithubOfficial
Copy link

haiderGithubOfficial commented Nov 23, 2023

great

@hughesjs
Copy link

hughesjs commented Dec 8, 2023

Going to come in with a controversial new option:

git branch --remote | cut -c 10- | xargs -d\\n -n1 git switch -f

Just make sure you've committed or stashed all of your changes. This does assume that your remote is called origin, if it's not, change the number of digits getting slashed by cut.

@andry81
Copy link

andry81 commented Dec 30, 2023

Mine own approach to pull and sync:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment