Pull and Push all branches of a repo along with its refs from one server to another
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.
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 namesrefs/remotes/*
all the remote-tracking namesrefs/tags/*
all the tag namesfurthermore, --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
Using the
--mirror
option seems to copy theremote
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
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.
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