Moving a Git Project

December 20, 2017

Sometimes, you might need to move your Git project to a new service or a new URL.  Using a few simple commands, you can move your project while preserving all of your previous commits.

Step 1 - Fetch All the Branches You Want To Keep

On your workstation, make sure you have ran git fetch and have the most recent version of all the branches you want to move.  At a minimum you'll need to move the master branch, but you can move any other branch your project has.

Also make sure that you have no uncommitted changes (git status shows no unstaged changes) and all of your commits are published to the remote repository (you've ran git push).

Step 2 - Update the Project URL

On your workstation, issue the following command to configure the new URL for the project.

git remote set-url origin [new-project-url]

Then push your copy of each branch you'd like to move.  You can specify as many branches you would like, just separate each with a space.

git push origin master [branch2 ...]

Alternatively, you can setup your new URL with a new remote.  This lets you access both URL while you transfer branches.  To do this, run the following command to add the new remote.

git remote add new-origin [new-project-url]

Then you can push changes of each branch.

git push new-origin master [branch2 ...]

When you're done, it's a good idea to set the origin URL to the new URL.  Do that by issuing the same command as before.

git remote set-url origin [new-project-url]

Step 3 - Verify

It's important to then login and make sure your project has all the files, commits, and branches you want before deleting it from the original location.  This way if you missed something you can still move them.