How to Migrate a Git Repository

Ivan Georgiev
2 min readMay 15, 2021

--

Sometimes you need to migrate an existing Git repository into a new repository. The migration needs to guarantee that the full commit history, branches, etc. is preserved.

In a blob post I provided a recipe on how to migrate Git repository and preserve the full commit history, branches, etc. Here I am providing a summary of the solution. If you want to learn more, check the full How to Migrate Git Repository with Full History and Branches post.

For example to migrate repository https://dev.azure.com/hemus/svetlina/_git/myapp to another repository https://dev.azure.com/asizen/svetlina/_git/newapp:

$ SOURCE_REPO=https://dev.azure.com/hemus/svetlina/_git/myapp
$ TARGET_REPO=https://dev.azure.com/asizen/svetlina/_git/newapp

Mirror clone the source repository

$ git clone --mirror $SOURCE_REPO
Cloning into bare repository 'myapp.git'...
remote: Azure Repos
remote: Found 79 objects to send. (24 ms)
Unpacking objects: 100% (79/79), done.

Push the mirror to the new repo

$ cd myapp.git$ git push --mirror $TARGET_REPO
Enumerating objects: 79, done.
Counting objects: 100% (79/79), done.
Delta compression using up to 8 threads
Compressing objects: 100% (61/61), done.
Writing objects: 100% (79/79), 7.77 KiB | 795.00 KiB/s, done.
Total 79 (delta 24), reused 0 (delta 0)
remote: Analyzing objects... (79/79) (7 ms)
remote: Storing packfile... done (325 ms)
remote: Storing index... done (86 ms)
To https://dev.azure.com/asizen/svetlina/_git/newapp
* [new branch] main -> main

And start using the new repository:

$ cd ..$ git clone $TARGET_REPO
Cloning into 'newapp'...
cd newapp
remote: Azure Repos
remote: Found 79 objects to send. (29 ms)
Unpacking objects: 100% (79/79), done.
$ cd newapp$ ls -lR
.:
total 4
drwxr-xr-x 1 ivang 197609 0 May 15 08:45 azure-pipelines/
-rw-r--r-- 1 ivang 197609 985 May 15 08:45 README.md
./azure-pipelines:
total 0
drwxr-xr-x 1 ivang 197609 0 May 15 08:45 pipelines/
drwxr-xr-x 1 ivang 197609 0 May 15 08:45 vars/
...

If you want to learn more or on this or other cloud engineering topics, check the full How to Migrate Git Repository with Full History and Branches post.

--

--