Relocate Remote Git Repository for a Local Clone

Master Git repository that my team used was moved to a new server. I wanted to keep my local clone and just switch remote reference and here’s how it can be done.

Show current remote (this is optional step of course):

1
2
3
4
5
6
7
$ git remote show origin
* remote origin
URL: [email protected]:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
Tracked remote branches
master

What we need to do is to remove current origin and add the new one:

1
2
3
4
5
6
7
8
9
10
$ git remote rm origin
$ git remote add origin [email protected]:project.git
$ git remote show origin
* remote origin
URL: [email protected]:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
error: refs/remotes/origin/HEAD points nowhere!
New remote branches (next fetch will store in remotes/origin)
master

Don’t worry about the error shown by the last command. The first pull from the origin will fix it:

1
2
3
4
5
6
7
8
9
10
11
$ git pull
From [email protected]:project.git
* [new branch]      master     -> origin/master
Already up-to-date.
$ git remote show origin
* remote origin
URL: [email protected]:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
Tracked remote branches
master

I’m still fresh with Git so maybe thre’s a better way to do it but this worked for me.

Comments