How to mirror a git repo to a new remote

Say you want to move a git repository from one remote (perhaps Github) to another (perhaps Gitlab).

If you have the repo checked out, you still should make sure to mirror all branches of the old remote, not only those you happen to have checked our. Otherwise the target repo will become a copy of your current repo, and not the source repo, potentially missing commits. You can use this:

git remote rename origin old-origin
git remote add origin <new-remote>
git fetch old-origin --prune
git push --prune origin +refs/remotes/old-origin/*:refs/heads/* +refs/tags/*:refs/tags/*

Note that the last push is effectively a "force push", and will set all branches on the new remote to exactly the branches on the old remote, even if your local branches are ahead.

To make sure, everything worked as expected, check that the latest revision on the old remote of some branch is present on the new remote.

Tobias Kraze Over 6 years ago