Skip to main content
Minor clarifications and style
Source Link

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 upstreamupstream 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 originorigin (refs/heads/<branch> on remote side).

Any of these branch sync commands may fail for one of two reasons: either the upstreamupstream 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 upstreamupstream.

As a complement to this answer, I was looking for a way to update all remote tracking branches of my cloned repo from upstream in one go. This is how I did it.

This assumes you have already configured an upstream remote pointing at the source repository and have synced it with git fetch upstream.

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 list all heads in the remote repo upstream and remove the SHA-1 and /refs/heads/ prefix.

Then for each of these branches, it will push the local copy of the remote branch (refs/remotes/upstream/<branch>) directly to the remote branch on origin (refs/heads/<branch>).

Any of these branch sync commands may fail for one of two reasons: either the 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 (git push -f). 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.

As a complement to this answer, I was looking for a way to update all remote branches of my cloned repo (origin) from upstream branches in one go. This is how I did it.

This assumes you have already configured an upstream remote pointing at the source repository (where origin was forked from) and have synced it with git fetch upstream.

Then 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 lists all heads in the upstream remote repo and removes the SHA-1 followed by refs/heads/ branch name prefix.

Then for each of these branches, it pushes the local copy of the upstream remote tracking branch (refs/remotes/upstream/<branch> on local side) directly to the remote branch on origin (refs/heads/<branch> on remote side).

Any of these branch sync commands may fail for one of two reasons: either the 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.

Source Link

As a complement to this answer, I was looking for a way to update all remote tracking branches of my cloned repo from upstream in one go. This is how I did it.

This assumes you have already configured an upstream remote pointing at the source repository and have synced it with git fetch upstream.

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 list all heads in the remote repo upstream and remove the SHA-1 and /refs/heads/ prefix.

Then for each of these branches, it will push the local copy of the remote branch (refs/remotes/upstream/<branch>) directly to the remote branch on origin (refs/heads/<branch>).

Any of these branch sync commands may fail for one of two reasons: either the 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 (git push -f). 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.