Moving A Git Repository To A New Server

Advertisement

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 documentation1, 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.

Moving A Git Repository To A New Server
Finally, an easy way of moving a git repository to a new server.

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
  remotes/origin/develop
  remotes/origin/master
  remotes/origin/release/0.1

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

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

  develop
  master
* release/0.1
  remotes/origin/develop
  remotes/origin/master
  remotes/origin/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 git@github.com:manakor/manascope.git

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.

(ds, il al)

Footnotes

  1. 1 http://git-scm.com/documentation

↑ Back to topShare on Twitter

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 innovative companies. Nik loves building software that scales using Google Closure Tools. He often shares his experience with younger programmers and learns a lot from great people surrounding him every day.

Advertising
  1. 1

    Thanks for the article. I’ll keep it in the bookmarks for the future.

    0
  2. 2

    It seems that this article makes it sound much more difficult than it is, unless I’m misunderstanding something (and I’ve just moved 30+ repositories, TWICE, in the last week).

    git clone –bare git@github.com:old/repo.git
    cd repo
    git remote add origin git@github.com:manakor/manascope.git
    git push –mirror

    The clone bare brings down all refs/tags/etc, and the push –mirror pushes everything back up.

    24
  3. 3

    There are easier and less error prone ways to move your repo. Look up
    `git clone –bare` and `git push –mirror`
    More details:
    https://help.github.com/articles/duplicating-a-repository#mirroring-a-repository

    4
  4. 4

    A raw combination of `git clone –bare` and `git push –mirror` would work if there is only a need to make an exact duplicate of the repository remotely. However, you anyway have to add a “new-origin” to avoid “fatal: remote origin already exists” error message, because `git clone –bare` clones a reference to the “origin” as well.
    Moreover, newly cloned local repository doesn’t contain a working tree, but a number of directories that represents Git refs/tags/etc, what requires you to delete it after doing mirror-push as pointed out in Github’s article: https://help.github.com/articles/duplicating-a-repository#mirroring-a-repository. What is the reason to delete the repository, if we just wish to keep working on it making sure all future commits are pushed to a new remote host.
    Still `git clone –bare` and `git push –mirror` is also a right way, but requires additional steps in between:
    `git clone –bare git@github.com:old/old_repo.git`
    `cd repo`
    `git remote add new-origin git@github.com:new/new_repo.git`
    `git push –mirror new-origin`
    `cd ..`
    `rm -rf repo`
    `git clone git@github.com:new/new_repo.git`

    In the end, we have 2 equally working ways: the one underlined in the article or mirroring unfold above. The one in the article doesn’t require you to delete the repository after changing its remote origin, allowing you to keep working with a working tree.

    1

Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top