How to change the URI (URL) for a remote Git repository?
git remote set-url {name} {url}
git remote set-url origin https://github.com/myName/GitTest.git
Change Host for a Git Origin Server
from: http://pseudofish.com/blog/2010/06/28/change-host-for-a-git-origin-server/
Hopefully this isn’t something you need to do. The server that I’ve been using to collaborate on a few git projects with had the domain name expire. This meant finding a way of migrating the local repositories to get back in sync.
Update: Thanks to @mawolf for pointing out there is an easy way with recent git versions (post Feb, 2010):
git remote set-url origin ssh://newhost.com/usr/local/gitroot/myproject.git
See the man page for details.
If you’re on an older version, then try this:
As a caveat, this works only as it is the same server, just with different names.
Assuming that the new hostname is newhost.com
, and the old one was oldhost.com
, the change is quite simple.
Edit the .git/config
file in your working directory. You should see something like:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = ssh://oldhost.com/usr/local/gitroot/myproject.git
Change oldhost.com
to newhost.com
, save the file and you’re done.
From my limited testing (git pull origin; git push origin; gitx
) everything seems in order. And yes, I know it is bad form to mess with git internals.
git remote -v
# View existing remotes
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
git remote set-url origin https://github.com/user/repo2.git
# Change the 'origin' remote's URL
git remote -v
# Verify new remote URL
# origin https://github.com/user/repo2.git (fetch)
# origin https://github.com/user/repo2.git (push)
Changing a remote's URL
You can
git remote set-url origin new.git.url/here
See git help remote
. You also can edit .git/config
and change the URLs there.
You're not in any danger of losing history unless you do something very silly (and if you're worried, just make a copy of your repo, since your repo is your history.)