Changing the origin of your git repository
July 16, 2008This is pretty basic and there's plenty of documentation on the web, but this morning I found it difficult to locate with a google search. Maybe I just didn't know the right terms. (searching "git repository change origin" seems obvious to me, but the results were useless.) Hopefully this post will help people find the documentation they are looking for if they are searching the same way I was.
Anyway, suppose you have a local git repository that you cloned from some remote repository at, say git://user@dev.foo.com/git/repos/path, and you've been happily pulling, pushing and rebaseing to and from that remote repository. But now you or your team leader have moved that remote repository to a new location, and you'd like to point your repository to the new URL without having to do a full checkout.
What you need to do is change your 'origin' setting. Unfortunately, there's no trivially easy way to do this from the command line in current versions of git. Instead, you edit .git/config in your project root, which may look something like this:
...
[remote "origin"]
url = git://user@dev.foo.com/git/repos/path
fetch = +refs/heads/*:refs/remotes/origin/*
...
or if your 'remote' is actually local:
...
[remote "origin"]
url = /path/to/repos/on/this/machine
fetch = +refs/heads/*:refs/remotes/origin/*
...
All you need to do is edit that file with your favorite editor and change the url = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll be happily pushing and pulling to and from your new remote location.
From the command line, while in the project:
git config remote.origin.url
Replace origin with any remote name to change the url for that remote.
Trying again:
git config remote.origin.url [new origin url]
It took me 10 minutes to figure out my command: “git config remote.origin.url=[new origin url]” had the extra “=”. No error message, no “git help config” example.
Git is a great tool, but like all open source stuff, far from complete. It’s all about productivity.
Great thank you. You saved us from a lot head scratching!
Thanks! Just what I was looking for!
Thanks Evan, very helpful indeed!
Add A Comment