Easy Git Repository Moving: 4 Common Scenarios and Solutions

If you landed on this article, you are probably staring at that rare occasion when you need to move one of your Git repositories.

You are either reorganizing existing repositories across servers, moving a repository between servers or hosting services, or just performing house cleaning of your local folder structure; there are several use cases that could warrant a repository move.

Here are some common scenarios and tactical techniques for each for migrating or moving a Git repository:

Moving a Git repo into an existing repo

Let’s assume you have two existing git repositories, both with their own content, and you want to “move” the contents of one repository (repo1) into the second repository (repo2).

Here are the git commands to help move a git repo under another existing repo –

cd repo2

git checkout master

git remote add r1remote **url-of-repo1**

git fetch r1remote

git merge r1remote/master --allow-unrelated-histories

git remote rm r1remote

After these commands are executed, repo2 will have all its original content plus repo1 content (including branches and history) you can delete repo1

(Source: stackoverflow.com)

Migrating a Git repository across servers

You may run into this situation if you are moving your git servers, or shutting down a particular git server. Here is the proper way to move your repository to another server while preserving branches and history:

git clone --mirror <URL to OLD repo location>

cd <New directory where OLD repo was cloned>

git remote set-url origin <URL to NEW repo location>

git push --mirror origin

(source: stackoverflow.com)

The –mirror option is used with the git clone command and creates a mirror copy of the source repository, which means the cloned local repo will have all the branches, tags, and git configuration as in the source repository.

After these commands are executed, navigate to <URL to new repo location> and confirm that you can see your repository at this location. At this point, you can safely remove the repo from the old server or shut down the old server

Copy a Git repository to another location without preserving the history

If you want to move or migrate your repo and you don’t care about history, you can modify the snippet in the previous section slightly. Instead of using –mirror option while cloning, this time, we can use –depth 1 option in the first command. The set of commands to then become:

git clone --depth 1 <URL to OLD repo location>

cd <New directory where OLD repo was cloned>

git remote set-url origin <URL to NEW repo location>

git push --mirror origin

–depth 1 creates a shallow clone of the repository with only the latest commit without pulling in the entire history.

Moving a Gitlab repository to another Gitlab location

If you are using Gitlab as your git server, gitlab recommends using the gitlab API to move Git repositories. The Moving GitLab repositories article from Gitlab lists the API calls and payloads for different scenarios, such as :

  • Moving git repositories between gitlab servers.
  • Moving git repositories between different storage.
  • Moving git repositories from a single-node Gitaly to a Gitaly Cluster
  • etc.


In this article, we discussed different scenarios and methods for migrating or moving a Git repository. I hope your scenario was covered. If not, let us know, and we’ll try to answer your question!