Categories
git git-remote url

How to change the URI (URL) for a remote Git repository?

5449

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

  • 8

    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, 2014 at 21:17

  • in my case i need to check my permission i have two private git repositories and this second account is admin of that new repo and first one is my default user account and i should grant permission to first

    Feb 6, 2020 at 16:35

  • Nice Doc is available here. docs.github.com/en/[email protected]/github/using-git/…

    Dec 28, 2020 at 8:49

8726

You can

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

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

19

  • 38

    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.: [email protected]://new.url.here

    – sobi3ch

    Jul 1, 2013 at 7:49


  • 19

    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, 2013 at 18:06


  • 38

    @kelorek or you can just git push -u origin master the first time 🙂

    – hobbs

    Aug 14, 2013 at 19:38

  • 45

    I also had to git remote set-url --push origin git://... in order to set the origin … (push) url.

    – jpillora

    Jun 2, 2014 at 9:12

  • 9

    For multiple branches, you can use git push -u --all to push all branches at once to new url (instead of git push -u origin master)

    – Ben

    Jan 14, 2018 at 16:11

1465

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

4

  • 3

    To get them all, I added: git remote set-url --push origin [email protected]/User/Branch.git and git remote set-url composer https://github.com/User/Branch.git

    – Reed

    Jul 14, 2020 at 18:56

  • 42

    @MS Berends The git remote -v helped for verification, whereas the accepted solution did not provide that.

    – rmutalik

    Nov 10, 2020 at 18:49

  • Note: if you’re getting 403 forbidden, follow this tutorial: janac.medium.com/…

    Sep 8, 2021 at 18:10

  • I did this, but after I did push with git add . && git commit -m 'fix' && git push origin to the new remote and it says everything is up-to-date while the new remote is empty. What could be the reason ?

    – Florent

    Jul 15 at 9:14

167

git remote set-url {name} {url}

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

0