I have a repo (origin) on a USB key that I cloned on my hard drive (local). I moved "origin" to a NAS and successfully tested cloning it from here.

I would like to know if I can change the URI of "origin" in the settings of "local" so it will now pull from the NAS, and not from the USB key.

For now, I can see two solutions:

  • push everything to the usb-orign, and copy it to the NAS again (implies a lot of work due to new commits to nas-origin);

  • add a new remote to "local" and delete the old one (I fear I'll break my history).

  • 3
    I had to do this on an old version of git (1.5.6.5) and the set-url option did not exist. Simply deleting the unwanted remote and adding a new one with the same name worked without problem and maintained history just fine. – HotN Sep 11 '14 at 21:17

11 Answers 11

up vote 4596 down vote accepted

You can

git remote set-url origin git://new.url.here

(see git help remote) or you can just 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.)

  • 16
    If you have a different shell user then maybe you want to specify your git user in the beginning of the new url e.g.: myself@git://new.url.here – sobi3ch Jul 1 '13 at 7:49
  • 8
    You may also want to set the master upstream branch for your new origin location with: git branch -u origin/master. This will allow you to just git push instead of having to git push origin master every time. – kelorek Aug 13 '13 at 18:06
  • 23
    @kelorek or you can just git push -u origin master the first time :) – hobbs Aug 14 '13 at 19:38
  • 13
    I also had to git remote set-url --push origin git://... in order to set the origin ... (push) url. – jpillora Jun 2 '14 at 9:12
  • 4
    use https:// instead of git:// – Anil Jul 22 '16 at 23:03
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

  • 38
    As a person scared of terminal due to its lack of visual feedback I highly appreciate your answer. – daniel.sedlacek Jun 4 '16 at 15:15
  • I prefer this answer. It is more complete. – Gabriel Villacis Aug 7 at 16:18
  • It is not because I dislike hobbs's answer, but I think with details is more understandable for everyone even for engineers (or not engineers) that are just starting. – Gabriel Villacis Aug 7 at 16:26

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.

  • Bad form? Perhaps. But if you need to do something the authors didn't expect anyone would ever need to do, then sometimes messing with the internals is required. But you do have to be willing to accept the consequences if you get it wrong. Backup your local repository _before_ messing with git internals. – Jesse Chisholm Oct 20 '16 at 15:41
git remote set-url origin git://new.location

(alternatively, open .git/config, look for [remote "origin"], and edit the url = line.

You can check it worked by examining the remotes:

git remote -v
# origin  git://new.location (fetch)
# origin  git://new.location (push)

Next time you push, you'll have to specify the new upstream branch, e.g.:

git push -u origin master

See also: GitHub: Changing a remote's URL

  • I could not set the new origin by editing .git/config. It said the git repository named in the URL wasn't a git repository. Once I removed and re-created origin, all was well. I had not looked up git remote set-url as a solution to my problem, though. – octopusgrabbus Sep 15 '15 at 13:55
  1. remove origin using command on gitbash git remote rm origin
  2. And now add new Origin using gitbash git remote add origin (Copy HTTP URL from your project repository in bit bucket) done

git remote set-url {name} {url}

ex) git remote set-url origin https://github.com/myName/GitTest.git

Switching remote URLs

Open Terminal.

Ist Step:- Change the current working directory to your local project.

2nd Step:- List your existing remotes in order to get the name of the remote you want to change.

3rd Step:- git remote -v

origin  https://github.com/USERNAME/REPOSITORY.git (fetch)

origin  https://github.com/USERNAME/REPOSITORY.git (push)

Change your remote's URL from HTTPS to SSH with the git remote set-url command.

4th Step:- git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Now Verify that the remote URL has changed.

5th Step:- git remote -v Verify new remote URL

origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
origin  git@github.com:USERNAME/REPOSITORY.git (push)

if you cloned your local will automatically consist,

remote URL where it gets cloned.

you can check it using git remote -v

if you want to made change in it,

git remote set-url origin https://github.io/my_repo.git

here,

origin - your branch

if you want to overwrite existing branch you can still use it.. it will override your existing ... it will do,

git remote remove url
and 
git remote add origin url

for you...

In the Git Bash, enter the command:

git remote set-url origin https://NewRepoLink.git

Enter the Credentials

Done

If you're using TortoiseGit then follow the below steps:

  1. Go to your local checkout folder and right click to go to TortoiseGit -> Settings
  2. In the left pane choose Git -> Remote
  3. In the right pane choose origin
  4. Now change the URL text box value to where ever your new remote repository is

Your branch and all your local commits will remain intact and you can keep working as you were before.

I worked:

git remote set-url origin <project>

protected by Tushar Gupta Nov 18 '14 at 17:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.