how to pull into multiple branches at once with git?
You can set up an alias that uses git fetch
with refspecs to fast-forward merge your branches with just one command. Set this up as an alias in your user .gitconfig
file:
[alias]
sync = "!sh -c 'git checkout --quiet --detach HEAD && \
git fetch origin master:master develop:develop ; \
git checkout --quiet -'"
Usage: git sync
.
Here is why it works:
git checkout --quiet HEAD
directly checks out your current commit, putting you into detached head state. This way, if you're onmaster
ordevelop
, you detach your working copy from those branch pointers, allowing them to be moved (Git won't allow you to move the branch references while your working copy has them checked out).git fetch origin master:master develop:develop
uses refspecs withfetch
to fast-forward themaster
anddevelop
branches in your local repo. The syntax basically tells Git "here is a refspec of the form<source>:<destination>
, take<destination>
and fast-forward it to the same point as<source>
". So the sources in the alias are the branches fromorigin
, while the destinations are the local repo versions of those branches.Finally,
git checkout --quiet -
checks out the branch you were last on, regardless of whether or not there was a failure in the previous commands. So if you were onmaster
when you rangit sync
, and everything succeeds, you'll leave detached head state and check out the newly updatedmaster
.
See also my answer to git: update a local branch without checking it out?.
Install git-up. It gives you the command git-up
which will pull all local branches in your repository.