I just ran into a situation where I needed to change a remote URL for a personal repository in Git. The project lived on a server at work, but I’m going to be going out of town for several weeks starting tomorrow. I need this project, and unfortunately, I can’t access it from home due to the work firewall.  What I decided to do is just move the repo to my personal server for now. Here’s how I did it (if it’s not obvious, I work over SSH).

First, I just wanted to see the current configuration:

~/Documents/project> git remote show origin
* remote origin
  Fetch URL: olduser@oldserver.com:/path/to/project.git
  Push  URL: olduser@oldserver.com:/path/to/project.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

Next, I need to SSH into the new server and create a new bare repo into which I’ll push my project. Since I store my git projects in /srv/git, I need to make sure I give the appropriate ownership to the project.

~$ cd /srv/git/
/srv/git$ sudo mkdir project.git
/srv/git$ sudo chown newuser:newuser project.git/
/srv/git$ cd project.git/
/srv/git/project.git$ git init --bare
Initialized empty Git repository in /srv/git/project.git/

The new server is now ready. All that’s left is for me to change the remote repo URL of the project on my local machine and then just push the project to the new server.

~/Documents/project> git remote set-url origin newuser@newserver.com:/srv/git/project.git
~/Documents/project> git push
Counting objects: 37567, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (37556/37556), done.
Writing objects: 100% (37567/37567), 88.91 MiB | 3.76 MiB/s, done.
Total 37567 (delta 4931), reused 0 (delta 0)
To newuser@newserver.com:/srv/git/project.git
 * [new branch]      master -> master

That’s it! All pushes/pulls from now on will happen with the new server. Pretty easy!