Notes

Clone and Push all branches

Pull and Push all branches of a repo along with its refs from one server to another

Edit on GitHub

Git & Github
2 minutes

tl;dr

1git clone --mirror URL
2cd NEW_REPO
3# git remote NEW_REMOTE URL
4# for remote in `git branch -r | grep -v '\->' | sed 's/ origin\///'`; do git branch --track $remote; done
5# git push NEW_REMOTE master -u 
6git push --mirror NEW_REMOTE_URL

--mirror is supposed to be used when you don’t have a working copy locally.


Clone the repo with all branch refs

Better to do this if you don’t have any local changes and are just migrating the branch from one palce to another.

--mirror updates all the refs as well

  • refs/heads/* all the branch names
  • refs/remotes/* all the remote-tracking names
  • refs/tags/* all the tag names

furthermore, --mirror implies both --force and --prune. locally updated refs will be force updated on the remote end, and locally deleted refs will be deleted from the remote as well.

1# Clone / Pull
2git clone --mirror repo_url
3
4# change to the new dir and push with --mirror to the new remote URL

This is a bare repo, which means your normal files won’t be visible, but it is a full copy of the Git database of your repository

setup mirrored branches locally

Using the --mirror option seems to copy the remote tracking branches properly. However, it sets up the repository as a bare repository, so you have to turn it back into a normal repository afterwards.

1git config --bool core.bare false
2# git checkout anybranch

track all the remotes locally

If you are moving branches to a new repo from an old one and do NOT have all the old repo branches local, you will need to track them first.

1# Track all repos locally
2for remote in `git branch -r | grep -v '\->' | sed 's/ origin\///'`; do git branch --track $remote; done

The tracking all repos step is necessary. Because even after having pushed all branches, Bitbucket won’t show any branches in the web admin, only the ones that are locally tracked show up in the dropdown.

push all the repos

Better to only push --mirror with repos that were cloned with --mirror as well

1# Push
2git push origin master -u
3git push origin --mirror
4
5# Push just all the local branches
6# git push new_origin --all