As a complement to this answer, I was looking for a way to update all remote tracking branches of my cloned repo (origin) from upstreamupstream branches in one go. This is how I did it.
This assumes you have already configured an upstream
upstream remote pointing at the source repository (where origin was forked from) and have synced it with git fetch upstream
.
RunThen run:
for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done
The first part of this command will listlists all heads in the upstream remote repo upstream
and removeremoves the SHA-1 andfollowed by /refs/heads/
branch name prefix.
Then for each of these branches, it will pushpushes the local copy of the upstream remote tracking branch (refs/remotes/upstream/<branch>
on local side) directly to the remote branch on origin
origin (refs/heads/<branch>
on remote side).
Any of these branch sync commands may fail for one of two reasons: either the upstream
upstream branch have been rewritten, or you have pushed commits on that branch to your fork. In the first case where you haven't committed anything to the branch on your fork it is safe to push forcefully (Add the -f switch; i.e. git push -f
in the command above). In the other case this is normal as your fork branch have diverged and you can't expect the sync command to work until your commits have been merged back into upstream
upstream.