Clone and Push all branches

tl;dr

git clone --mirror URL
cd NEW_REPO
# git remote NEW_REMOTE URL
# for remote in `git branch -r | grep -v '\->' | sed 's/ origin\///'`; do git branch --track $remote; done
# git push NEW_REMOTE master -u 
git 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.

# Clone / Pull
git clone --mirror repo_url

# 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.

git config --bool core.bare false
# 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.

# Track all repos locally
for 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

# Push
git push origin master -u
git push origin --mirror

# Push just all the local branches
# git push new_origin --all