Migrate from one git repo to another

Posted on Sat 14 December 2019 in git

It does happen from time to time that you need to migrate an existing git repo to a new remote repo. Doing some googling I found the following steps to work best for me.

First make sure you have a local copy of all the old repo branches and tags, so fetch it.

git fetch origin

Check to see which branches of the old repo you don’t have a local copy of:

git branch -a

If you find that some of the remote branches don’t have any local copies, check them out to create a local copy:

git checkout -b <branch> origin/<branch>

Now that all the branches have a local copy, add the new repo as a new remote origin:

git remote add new-origin git@your-repo-host.com:user/repo.git

Push all the local branches to new-origin:

git push --all new-origin

Next push all the tags:

git push --tags new-origin

Now you can get rid of the old repo origin and all its dependencies and rename the new origin to replace the old:

git remote rm origin
git remote rename new-origin origin

And your done! All your commits and tags have now been migrated to the new-repo ready to be used.

git