I often have at least 3 remote branches: master, staging and production. I have 3 local branches that track those remote branches.
Updating all my local branches is
The following one-liner fast-forwards all branches that have an upstream branch if possible, and prints an error otherwise:
git branch \
--format "%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)" |
sh
It uses a custom format with the git branch
command. For each branch that has an upstream branch, it prints a line with the following pattern:
git push . :
This can be piped directly into sh
(assuming that the branch names are well-formed). Omit the | sh
to see what it's doing.
The one-liner will not contact your remotes. Issue a git fetch
or git fetch --all
before running it.
The currently checked-out branch will not be updated with a message like
! [remote rejected] origin/master -> master (branch is currently checked out)
For this, you can resort to regular git pull --ff-only
.
Add the following to your .gitconfig
so that git fft
performs this command:
[alias]
fft = !sh -c 'git branch --format \"%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)\" | sh' -
See also my .gitconfig. The alias is a shorthand to "fast-forward tracking (branches)".