Migrate Git Repository With History

Oğuz Kaan Kahraman
2 min readAug 1, 2020

--

Git migration

Please check my blog if you would like to read my article in Turkish.

If you want to jump directly to solution without learning the steps, have a look at my bash script on github via the link below. It is super easy to use.

When we say version control, the technology that comes to our mind no doubt is git. Best of all, git is linux’s step brother :) https://www.linuxfoundation.org/blog/2015/04/10-years-of-git-an-interview-with-git-creator-linus-torvalds/

If you want to switch from github to gitlab or from an older gitlab version to a new gitlab version (gitlab backup restore does not support different versions) and you do not want to lose history, tag, branch etc. while doing so, all you need is to install either git on linux or git-bash on windows for instance.

Let’s Start!

1- We all used, git clone one way or another. But clone with mirror ?

git clone --mirror old-repo-url new-repo

Copy&Paste from doc.

Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository

Self explaining, is it not ?

2- (optional) Get rid of the old one. We need some fresh air.

cd new-repo
git remote remove origin

Note: Actually, as i mentioned, this step is not necessary. You can be connected to more than one remote repos. But doing this will make your commands easier in the future. It also reduces management costs. Apart from that, if you are reading this article for backup, you can skip this step as I said.

3- This step has a really important requirement. Before executing this step please make sure you have created the git repo on the new host!

git remote add origin new-repo-url

4- Mix on low heat until boiling :)

git push --all
git push --tags

We have pushed all the history, tags, branches etc. Migration is over. But not this article, not yet.

The clone performed in the first step cannot be used for new commits. If you skip opcoming step, you cannot make new commits and push them.

5- Clean-up and get the project from the new host.

cd ..
rm -rf new-repo
git clone new-repo-url new-repo

--

--