Transfer Git repositories with LFS content, all branches and complete history

As we were forced to move our GIT repositories from a cloud hosting service to our servers on site I wrote a litte shellscript for OSX to do this for me repository by repository.

The main problem was to transfer the LFS content wich wasn’t supported by our cloud hosting service so we had to check out everything and push it to the new hoster. This came with new problems like transferring all branches and tags.

But luckily there is an answer for everything which i’d like to share if someone runns into similar problems!

First of i request the projectname (reponame) and the old and new git repo url. I did this with ssh but https auth should work aswell


read -p "Enter project name to Clone: "  projectname
read -p "Enter old Git URL(ssh): "  oldgiturl
read -p "Enter NEW Git URL(ssh): "  newgiturl

Then i ran git clone with the mirror argument to completely copy the repo with all branches.

git clone --mirror --recurse-submodules $oldgiturl
cd $projectname
git config --bool core.bare false
git checkout
git lfs fetch --all origin

After checking everything out you can download the LFS content

git remote add new-origin $newgiturl
git push -u new-origin --all
git push --tags new-origin
git lfs push --all new-origin

After this you add a new origin and push everything to it.
Repeat for every repository you want to move.

If you have submodules in your projects like me, you can alter the script and have them mapped to the new hoster as well.
Luckily for me we have always the same project layout so checking the submodules was easy.
Like this:

[ -d "Assets/Submodules/base_loca" ] && git rm -rf /Assets/Submodules/base_loca && git submodule add --name base_loca **insertnewhosterurlhere**/base_loca.git Assets/Submodules/base_loca

Here i checked if the submodule exists in the downloaded content, if so, i remove it and add the corresponding submodule which was already transferred beforhand. These submodule repacement lines of code has to be repeated for every submodule you have and are hardcoded in the script.
Maybe this can be done more efficient but it worked for us.

I hope this helps you to do this kind of monkey job a bit faster 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: