6037

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 origin, 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).

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • 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 '14 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 – saber tabatabaee yazdi Feb 06 '20 at 16:35
  • Nice Doc is available here. https://docs.github.com/en/free-pro-team@latest/github/using-git/changing-a-remotes-url – Ravi Parekh Dec 28 '20 at 08:49
  • The answers here show the easiest way to clone a remote repo locally (if the web console does not allow it, like in on-premise Bitbucket, where the import feature is flawed - you cannot clone existing repo under a new name in the same namespace). – mirekphd Aug 07 '22 at 11:32

34 Answers34

9617

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.)

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • 42
    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 01 '13 at 07:49
  • 20
    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
  • 41
    @kelorek or you can just `git push -u origin master` the first time :) – hobbs Aug 14 '13 at 19:38
  • you'll probably have to do a git pull to merge contents after that: http://stackoverflow.com/questions/1713137/how-to-merge-remote-changes-at-github – Alejandro Moreno Sep 18 '13 at 09:15
  • 50
    I also had to `git remote set-url --push origin git://...` in order to set the origin ... (push) url. – jpillora Jun 02 '14 at 09:12
  • This gives a warning that `remote.origin.url has multiple values` ... how do i get rid of the old value? – Michael Jul 03 '17 at 19:16
  • 11
    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 '18 at 16:11
  • In the case of ssh, you will get the URL in your repo. You have to use exactly the same, unless that it doesn't work. expl: git@github.com:reponame.git – Mitesh Ukate Aug 31 '18 at 09:51
  • I always find myself coming back to this and making the same mistake. This is a very old post without edits and this may have been required before, but the "git://" is unnecessary now (and actually doesn't work). I'd suggest changing "git://new.url.here" to something more obvious such as "[NEW URL]," because this seems to be confusing others as well considering upvotes on newer comment. – mauriii Jan 03 '19 at 07:29
  • Please include Ben's additional optional command to update all branches (if user desires). Setting the new origin is great, but without pushing your history to the repo, the remote is out of sync by one or more branches. – Turbo Nov 09 '19 at 01:45
  • 1
    When I tried this I got `fatal: No such remote 'origin'` I could only get it to work after running `git remote add origin new.git.url/here` then `git push --set-upstream origin master` – jbobbins Mar 17 '20 at 23:49
  • If you are using Intellij Idea, simply open the terminal and change the command. It will work right there to set the origin. Thanks for sharing this! – Aadil Mar 27 '20 at 08:33
  • NOTE: Worked, but I lost all files changes that were listed in `git status` when I changed it. Since I do not know git very well, I re-cloned. copied files that I changed and then committed. – B. Shea Apr 21 '20 at 18:22
  • This another answer worked for me for the existing https repos. https://superuser.com/a/1082680 – Faiyaz Alam Apr 11 '21 at 17:38
  • To change user on new remote: `git config [--global] user.name "Full Name"` `git config [--global] user.email "email@address.com"` – Cosmo Dai Jun 03 '21 at 02:25
  • For me, I couldn't use my git username, but was asked to use "git@github" for SSH to work, as written here: https://docs.github.com/en/github/authenticating-to-github/troubleshooting-ssh/error-permission-denied-publickey#always-use-the-git-user – Narxx Jul 11 '21 at 08:29
  • Note: if you're getting 403 forbidden, follow this tutorial: https://janac.medium.com/pushing-to-git-returning-error-code-403-fatal-http-request-failed-bcdcb5c0732 – Janac Meena Sep 08 '21 at 18:10
  • It's not clear to when you do: `git remote set-url origin new.git.url/here` and then `git push -u --all` you will lose non-local branch information – matteogll Apr 28 '22 at 12:07
  • notice to set the url with a `.git` at the end, `https://github.com/arye321/django-allauth` didn't work for me, but `https://github.com/arye321/django-allauth.git` worked. – Arye P. Jul 07 '22 at 07:02
1676
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

Zombo
  • 1
  • 62
  • 391
  • 407
Utensil
  • 17,204
  • 1
  • 17
  • 10
  • 4
    To get them all, I added: `git remote set-url --push origin git@github.com/User/Branch.git` and `git remote set-url composer https://github.com/User/Branch.git` – Reed Jul 14 '20 at 18:56
  • 52
    @MS Berends The `git remote -v` helped for verification, whereas the accepted solution did not provide that. – rmutalik Nov 10 '20 at 18:49
  • Note: if you're getting 403 forbidden, follow this tutorial: https://janac.medium.com/pushing-to-git-returning-error-code-403-fatal-http-request-failed-bcdcb5c0732 – Janac Meena Sep 08 '21 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 '22 at 09:14
192

git remote set-url {name} {url}

git remote set-url origin https://github.com/myName/GitTest.git
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
최봉재
  • 4,039
  • 3
  • 16
  • 21
  • 5
    What have you added with this answer to the accepted answer, which was accepted five years before yours? You're also assuming this relates to github, which the asker didn't specify. You should delete. – mike rodent Aug 11 '22 at 19:12
138

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.

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
yoda
  • 4,859
  • 3
  • 19
  • 9
  • 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
  • where I can find .git/config – Abdulmalek Oct 08 '20 at 15:36
  • The pseudofish blog entry has moved and is now located at http://pseudofish.com/change-host-for-a-git-origin-server.html – DDay Mar 29 '23 at 16:01
117

This is very easy and simple; just follow these instructions.

  1. For adding or changing the remote origin:
    git remote set-url origin githubrepurl
    
  2. To see which remote URL you have currently in this local repository:
    git remote show origin
    
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Mithun Rana
  • 1,334
  • 1
  • 9
  • 10
88

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.

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.

3rd Step:- git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

4th Step:- Now Verify that the remote URL has changed.

git remote -v Verify new remote URL

origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
origin  git@github.com:USERNAME/REPOSITORY.git (push)
Community
  • 1
  • 1
VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
82
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

Zaz
  • 46,476
  • 14
  • 84
  • 101
  • 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
  • 3
    +1 for providing a complete answer with the `git push -u` command. Maybe obvious to others, wasn't for me. – testphreak Sep 20 '19 at 21:51
64

As seen here,

$ git remote rm origin
$ git remote add origin git@github.com:aplikacjainfo/proj1.git
$ git config master.remote origin
$ git config master.merge refs/heads/master
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Zahid Hassan Shaikot
  • 1,066
  • 10
  • 18
  • 25
    When adding an answer to a ten year old question with twenty one other answers it is really important to include an explanation of your answer and to point out what new aspect of the question your answer addresses. With answers that are a series of commands it is useful to explain what each is doing and how to undo the effects of each of them if that is possible. The undo is important in case someone is able to perform the first few steps, but then encounters an error on a later step. – Jason Aller Apr 02 '20 at 16:01
  • 7
    @JasonAller I think this is fairly self-explanatory though and it's the best answer here by far, the others are a joke. – Oliver Dixon May 13 '20 at 13:07
  • this will also update *git status* **Your branch is ahead of 'origin/master' by n commits** to the new one – John Balvin Arias Oct 16 '21 at 13:26
  • 1
    TKS. You help me. :) – acacio.martins Jun 28 '23 at 20:03
43
  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
Sunil Chaudhary
  • 1,221
  • 12
  • 25
  • This is really useful answer because without `git remote rm origin` git remembers details about the old origin. – mato Dec 11 '19 at 08:02
  • 1
    The above `git remote rm origin` resolves the issue of multiple remotes: issue where I was not able to set the remote url. `remote.origin.url has multiple values fatal: could not set 'remote.origin.url'` – bitsand Dec 26 '19 at 15:47
  • @mato like what details? – theonlygusti Feb 23 '22 at 22:54
41

It will work fine, you can try this

For SSH:

command: git remote set-url origin <ssh_url>

example: git remote set-url origin git@github.com:username/rep_name.git

For HTTPS:

command: git remote set-url origin <https_url>

example: git remote set-url origin https://github.com/username/REPOSITORY.git

MD. SHIFULLAH
  • 913
  • 10
  • 16
40

First you need to type this command to view existing remotes

git remote -v

Then second you need to type this command to Change the 'origin' remote's URL

git remote set-url origin <paste your GitHub URL>

arnavakhoury
  • 125
  • 1
  • 8
35

Write the below command from your repo terminal:

git remote set-url origin git@github.com:<username>/<repo>.git

Refer this link for more details about changing the url in the remote.

viveknaskar
  • 2,136
  • 1
  • 20
  • 35
30

To check git remote connection:

git remote -v

Now, set the local repository to remote git:

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

Now to make it upstream or push use following code:

git push --set-upstream origin master -f

Anupam Maurya
  • 1,927
  • 22
  • 26
27

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...

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • 1
    I had multiple remotes added, so `git remote rm origin` command was needed for removing all the associated urls. Then the add command worked. – bitsand Dec 26 '19 at 15:44
27

enter image description here

Troubleshooting :

You may encounter these errors when trying to changing a remote. No such remote '[name]'

This error means that the remote you tried to change doesn't exist:

git remote set-url sofake https://github.com/octocat/Spoon-Knife fatal: No such remote 'sofake'

Check that you've correctly typed the remote name.

Reference : https://help.github.com/articles/changing-a-remote-s-url/

Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
27

Navigate to the project root of the local repository and check for existing remotes:

git remote -v

If your repository is using SSH you will see something like:

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

And if your repository is using HTTPS you will see something like:

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

Changing the URL is done with git remote set-url. Depending on the output of git remote -v, you can change the URL in the following manner:

In case of SSH, you can change the URL from REPOSITORY.git to NEW_REPOSITORY.git like:

$ git remote set-url origin git@github.com:USERNAME/NEW_REPOSITORY.git

And in case of HTTPS, you can change the URL from REPOSITORY.git to NEW_REPOSITORY.git like:

$ git remote set-url origin https://github.com/USERNAME/NEW_REPOSITORY.git

NOTE: If you've changed your GitHub username, you can follow the same process as above to update the change in the username associated with your repository. You would only have to update the USERNAME in the git remote set-url command.

Saurabh
  • 5,176
  • 4
  • 32
  • 46
25

I worked:

git remote set-url origin <project>
23

For me, the accepted answer worked only in the case of fetch but not pull. I did the following to make it work for push as well.

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

So to update the fetch URL:

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

To update the pull URL:

git remote set-url --push origin new.git.url/here
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
22

In the Git Bash, enter the command:

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

Enter the Credentials

Done

devDeejay
  • 5,494
  • 2
  • 27
  • 38
21

You have a lot of ways to do that:

Console

git remote set-url origin [Here new url] 

Just be sure that you've opened it in a place where a repository is.

Config

It is placed in .git/config (same folder as repository)

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = [Here new url]  <------------------------------------
...

TortoiseGit

Step 1 - open settings

Step 2 - change url

Then just edit URL.

SourceTree

  1. Click on the "Settings" button on the toolbar to open the Repository Settings window.

  2. Click "Add" to add a remote repository path to the repository. A "Remote details" window will open.

  3. Enter a name for the remote path.

  4. Enter the URL/Path for the remote repository

  5. Enter the username for the hosting service for the remote repository.

  6. Click 'OK' to add the remote path.

  7. Back on the Repository Settings window, click 'OK'. The new remote path should be added on the repository now.

  8. If you need to edit an already added remote path, just click the 'Edit' button. You should be directed to the "Remote details" window where you can edit the details (URL/Path/Host Type) of the remote path.

  9. To remove a remote repository path, click the 'Remove' button

enter image description here

enter image description here

ref. Support

Przemek Struciński
  • 4,990
  • 1
  • 28
  • 20
20

Change remote git URI to git@github.com rather than https://github.com

git remote set-url origin git@github.com:<username>/<repo>.git

Example:

git remote set-url origin git@github.com:Chetabahana/my_repo_name.git

The benefit is that you may do git push automatically when you use ssh-agent :

#!/bin/bash

# Check ssh connection
ssh-add -l &>/dev/null
[[ "$?" == 2 ]] && eval `ssh-agent`
ssh-add -l &>/dev/null
[[ "$?" == 1 ]] && expect $HOME/.ssh/agent

# Send git commands to push
git add . && git commit -m "your commit" && git push -u origin master

Put a script file $HOME/.ssh/agent to let it runs ssh-add using expect as below:

#!/usr/bin/expect -f
set HOME $env(HOME)
spawn ssh-add $HOME/.ssh/id_rsa
expect "Enter passphrase for $HOME/.ssh/id_rsa:"
send "<my_passphrase>\n";
expect "Identity added: $HOME/.ssh/id_rsa ($HOME/.ssh/id_rsa)"
interact
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
eQ19
  • 9,880
  • 3
  • 65
  • 77
18

To change the remote upstream: git remote set-url origin <url>


To add more upstreams: git remote add newplace <url>

So you can choose where to work git push origin <branch> or git push newplace <branch>

Anderson Cossul
  • 261
  • 2
  • 6
14

You can change the url by editing the config file. Go to your project root:

nano .git/config

Then edit the url field and set your new url. Save the changes. You can verify the changes by using the command.

git remote -v 
Abhi Das
  • 500
  • 8
  • 11
13

An alternative approach is to rename the 'old' origin (in the example below I name it simply old-origin) and adding a new one. This might be the desired approach if you still want to be able to push to the old origin every now and then:

git remote rename origin old-origin
git remote add origin git@new-git-server.com>:<username>/<projectname>.git

And in case you need to push your local state to the new origin:

git push -u origin --all
git push -u origin --tags
j-i-l
  • 10,281
  • 3
  • 53
  • 70
12

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Vipul bhojwani
  • 366
  • 4
  • 7
11

Go to the folder/repo where you want to change and execute the below commands:

The below command will change the git fetch url of the repo.

git remote set-url origin <your new url>.git

The below command will change the git push url of the repo.

git remote set-url --push origin <your new url>.git

The below command to check whether the above changes reflected or not

git remote -v
SANDEEP MACHIRAJU
  • 817
  • 10
  • 17
9

Removing a remote

Use the git remote rm command to remove a remote URL from your repository.

    $ git remote -v
    # View current remotes
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)
    > destination  https://github.com/FORKER/REPOSITORY.git (fetch)
    > destination  https://github.com/FORKER/REPOSITORY.git (push)

    $ git remote rm destination
    # Remove remote
    $ git remote -v
    # Verify it's gone
    > origin  https://github.com/OWNER/REPOSITORY.git (fetch)
    > origin  https://github.com/OWNER/REPOSITORY.git (push)
Tayyab Roy
  • 423
  • 5
  • 7
9

If you would like to set the username and password as well in the origin url, you can follow the below steps.

Exporting the password in a variable would avoid issues with special characters.

Steps:

export gituser='<Username>:<password>@'
git remote set-url origin https://${gituser}<gitlab_repo_url> 
git push origin <Branch Name>
Rajesh Somasundaram
  • 448
  • 1
  • 4
  • 13
6

check your privilege

in my case i need to check my username

i have two or three repository with seperate credentials.

problem is my permission i have two private git server and repositories

this second account is admin of that new repo and first one is my default user account and i should grant permission to first

saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58
5

(Only Windows PS) To change a server/protocol recursively in all local repos

Get-ChildItem -Directory -Recurse -Depth [Number] -Hidden -name | %{$_.replace("\.git","")} | %{git -C $_ remote set-url origin $(git -C $_ remote get-url origin).replace("[OLD SERVER]", "[NEW SERVER]")}
bruegth
  • 461
  • 5
  • 13
4

For those who want to make this change from Visual Studio 2019

Open Team Explorer (Ctrl+M)

Home -> Settings

Git -> Repository Settings

Remotes -> Edit

enter image description here

Dinch
  • 548
  • 4
  • 9
4

If your repository is private then

  1. Open Control Panel from the Start menu
  2. Select User Accounts
  3. Select "Manage your credentials" in the left hand menu
  4. Delete any credentials related to Git or GitHub

Reference

Mike Lee
  • 2,440
  • 26
  • 12
4

This answer pertains to a question I wasn't able to find an answer for elsewhere: doing this for a project in Gitlab, which has a subgroup. This did work via SSH:

git remote add origin git@gitlab.com:group/subgroup/project.git

In contrast to all the answers which have "username" instead of "group", none of those worked for me via SSH. Alternatively, what worked through https:

git remote add origin https://username@gitlab.com/group/subgroup/project.git

Do note the difference in the two with ".com:group" and ".com/group"

If this doesn't work right out of the box, I used the HTTPS method before successfully using the SSH one, might be a factor but I can't be arsed to try and replicate it without it.

m_highlanderish
  • 499
  • 1
  • 6
  • 16
  • Thank you, I downloaded repo from zip file instead of cloning it, using ```git remote set-url``` does not work, only with add was possible to sync files. – Renan Ribeiro May 31 '23 at 12:36
0

If you’re on Linux or Mac with sed/grep, it’s also pretty easy to change without losing associations.

Recently I did this to change my username for all my repositories locally in the configuration, but I have done the same approach for remotes lines entirely too.

VARIABLE_FIND='old'; VARIABLE_REPLACE='new'; path_name='~/PycharmProjects/*/.git/'; grep -rl --include=config "${VARIABLE_FIND}" "${path_name}" | xargs sed -i "s|${VARIABLE_FIND}|${VARIABLE_REPLACE}|g"

For replacing whole lines where there is a match, you can do this:

VARIABLE_FIND='someneedle'; VARIABLE_REPLACE='somenewvalue'; path_name='/home/*/' grep -rl --include=config "${VARIABLE_FIND}" "${path_name}" | xargs sed -i "/${VARIABLE_FIND//\//\\/}/c\\${VARIABLE_REPLACE}" ;

Reference: Replace whole line containing a string using Sed

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike R
  • 679
  • 7
  • 13