Moving A Git Repository To A New Server

About The Author

Nik Sumeiko is a JavaScript hacker and Frontend infrastructure engineer based in Vienna, Austria. He works freelance with teams at Sony, IDEO, Samsung and other … More about Nik ↬

Email Newsletter

Weekly tips on front-end & UX.
Trusted by 200,000+ folks.

Suppose your company decides to change its code-hosting provider or you wish to move your own Git repository to a different host. It doesn’t happen often, but it happens.

When I had to move a number of Git projects to a new host, it took me quite some time to find an accurate method. Having made many attempts, and a couple of fails, and carefully reading Git’s documentation, I found a solid and effective way. I thought, then, that every developer would benefit from knowing how to migrate a Git repository to a new host quickly and easily. The most important thing is to make sure that your branches and tags and your commit history are all moved.

Let’s Learn How To Do That Properly

First, we have to fetch all of the remote branches and tags from the existing repository to our local index:

git fetch origin

But even if all branches and tags have been fetched and exist in a local index, we still won’t have a copy of them that is physically local. And a local copy is required to migrate the repository.

We can check for any missing branches that we need to create a local copy of. Let’s list all existing branches (local and remote) to see whether we are missing any copies locally:

git branch -a

* master

We can easily tell from this output whether we have local copies of all remote branches: The remote ones are prefixed with the remotes/origin/ path, and the local ones aren’t. So, only our master branch is local, whereas remotes/origin/develop and remotes/origin/release/0.1 are not. That’s OK — let’s just create local copies:

git checkout -b develop origin/develop
git checkout -b release/0.1 origin/release/0.1
More after jump! Continue reading below ↓

After creating local copies of everything, we can verify once again whether all branches with the remotes/origin/ prefix have corresponding local copies (shown without the prefix):

git branch -a

* release/0.1

Now we know for sure that all branches in our repository are stored locally, and we are ready to move the repository to a new host.

At this point, let’s assume we have an SSH-cloned URL of our new repository. If not, create a new repository, and then you will have its SSH-cloned URL.

Let’s use the SSH-cloned URL of our new repository to create a new remote in our existing local repository by executing the following command:

git remote add new-origin

This will give us two remotes for the existing repository: the original one (named origin, connected to the existing remote host) and a new one (named new-origin, connected to the new host).

(Git has this concept of “remotes,” which are simply URLs to other copies of your repository. The name origin refers to the original remote repository; by convention, it is considered the primary centralized repository.)

Now we are ready to push all local branches and tags to the new remote named new-origin. We could push each local branch separately, but pushing all branches at once would be much faster by executing the following Git command:

git push --all new-origin

Let’s also push all of the tags to new-origin with this Git command:

git push --tags new-origin

We’re almost done. Let’s make new-origin the default remote, which will direct all future commits to the new repository that we have just pushed to. To do this, remove the old repository remote:

git remote rm origin

And rename new-origin to just origin, so that it becomes the default remote:

git remote rename new-origin origin

(Everyone is used to the default remote being named origin. So, let’s keep it standard by naming our primary centralized repository remote origin, too.)

Awesome, Done!

Your repository is now connected to the new remote (i.e. the new host), which will store all of your branches and tags and your commit history. Plus, all commits will be pushed to the new remote host from now on.

Further Reading on SmashingMag:

Smashing Editorial (ds, al, il)