June 3, 2018 · Code Snippet DevOps Tutorial

Migrate remote servers in git repositories

With news like Microsoft buying GitHub people might migrate their repositories between services or add them to organizations.

The git design features a remote repository where our commits are stored (like GitHub, GitLab or Bitbucket). The default remote repository is called origin.

We have two choices: Overwrite the origin destination URL, or make a new remote in our git repository.

Now we will start the process to migrate our repo.

Do a fresh clone of your current repo

Clone the repo to migrate to a temporary directory:

cd /tmp
git clone git@github.com:username/myrepo.git

Migrate the repo

In your new git remote service (or organization) create a blank repository, as an example, we will assume git@gitlab.com:username/myrepo.git.

Overwrite origin destination (recommended)

Go to your local repo directory, change the origin URL and push changes:

cd /tmp/myrepo
git remote set-url origin git@gitlab.com:username/myrepo.git
git push -u origin master

Add new remote repository

Go to your local repo directory, add a new repository URL (in our case, we call it backup) and push changes:

cd /tmp/myrepo
git remote add backup git@gitlab.com:username/myrepo.git
git push -u origin backup

Verify your changes

You can verify your remotes in your local repo with:

git remote -v

And also be sure to check your new remote repository in their website. :)

Enjoy!