-
-
Save grimzy/a1d3aae40412634df29cf86bb74a6f72 to your computer and use it in GitHub Desktop.
#!/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 |
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
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
when I executed git branch -r | grep -v '\->'
, I got lots of previous branches which not used yet. why?
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.
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
If there are existing branch names, e.g.
master
, thenset -e
option will cause this command fails and some branches may not be checkout.
This issue can be fixed by appending|| true
forgit 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!
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
Hello and thanks everyone for this tips
Thank you so much ElfSundae, I've looked for this answer so much, only to find fetch --all
...
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
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.
Thank you for this! It was very helpful.
thank you
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
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
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?
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
@jcwren thanks, I fixed it
This was so helpful! Thank you!
great
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.
Mine own approach to pull and sync:
- https://github.com/andry81/gitcmd (implementation, not interactive usage)
git_pull_remote*.sh
git_sync_remotes.sh
- https://github.com/andry81/gituserbin (wrappers, interactive usage)
pull-remote*.*
sync-remotes.*
--all
just means to fetch from all remotes.