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

GitUrlGit Remote

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

git remote set-url {name} {url}

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

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

Solution 5 - Git

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

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

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 [email protected]: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)

Solution 7 - Git

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. For which remote URL you have currently in this local repository:
    git remote show origin
    

Solution 8 - Git

As seen here,

$ git remote rm origin
$ git remote add origin [email protected]:aplikacjainfo/proj1.git
$ git config master.remote origin
$ git config master.merge refs/heads/master

Solution 9 - Git

  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

Solution 10 - Git

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.

Solution 11 - Git

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

Solution 12 - Git

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

Solution 13 - Git

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.

Solution 14 - Git

I worked:

git remote set-url origin <project>

Solution 15 - Git

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/

Solution 16 - Git

You can rename a repository if you're either an organization owner or have admin permissions for the repository.

 git remote set-url origin new_url

know more

Solution 17 - Git

In the Git Bash, enter the command:

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

Enter the Credentials

Done

Solution 18 - Git

> Change remote git URI to [email protected] 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

Solution 19 - Git

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

Solution 20 - Git

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>

Solution 21 - Git

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

Solution 22 - Git

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 

Solution 23 - Git

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

Solution 24 - Git

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.

Solution 25 - Git

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)

Solution 26 - Git

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>

Solution 27 - Git

First you need to type this command to view existing remotes

  1. git remote -v

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

  1. git remote set-url origin paste your GitHub URL

Solution 28 - Git

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

Solution 29 - Git

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

Solution 30 - Git

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

Solution 31 - Git

(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]")}

Solution 32 - Git

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: https://stackoverflow.com/questions/11245144/replace-whole-line-containing-a-string-using-sed

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questione-satisView Question on Stackoverflow
Solution 1 - GithobbsView Answer on Stackoverflow
Solution 2 - GitUtensilView Answer on Stackoverflow
Solution 3 - Git최봉재View Answer on Stackoverflow
Solution 4 - GityodaView Answer on Stackoverflow
Solution 5 - GitZazView Answer on Stackoverflow
Solution 6 - GitVIKAS KOHLIView Answer on Stackoverflow
Solution 7 - GitMithun RanaView Answer on Stackoverflow
Solution 8 - GitZahid Hassan ShaikotView Answer on Stackoverflow
Solution 9 - GitSunil ChaudharyView Answer on Stackoverflow
Solution 10 - GitviveknaskarView Answer on Stackoverflow
Solution 11 - GitAnupam MauryaView Answer on Stackoverflow
Solution 12 - GitMohideen bin MohammedView Answer on Stackoverflow
Solution 13 - GitSaurabhView Answer on Stackoverflow
Solution 14 - GitDiego Santa Cruz MendezúView Answer on Stackoverflow
Solution 15 - GitAmitesh BhartiView Answer on Stackoverflow
Solution 16 - GitMD SHAYONView Answer on Stackoverflow
Solution 17 - GitdevDeejayView Answer on Stackoverflow
Solution 18 - GiteQ19View Answer on Stackoverflow
Solution 19 - GitPrzemek StrucińskiView Answer on Stackoverflow
Solution 20 - GitAnderson CossulView Answer on Stackoverflow
Solution 21 - GitShailendra MaddaView Answer on Stackoverflow
Solution 22 - GitAbhi DasView Answer on Stackoverflow
Solution 23 - GitjojoView Answer on Stackoverflow
Solution 24 - GitVipul bhojwaniView Answer on Stackoverflow
Solution 25 - GitTayyab RoyView Answer on Stackoverflow
Solution 26 - GitRajesh SomasundaramView Answer on Stackoverflow
Solution 27 - GitAbdullah Al MahmudView Answer on Stackoverflow
Solution 28 - Gitsaber tabatabaee yazdiView Answer on Stackoverflow
Solution 29 - GitDinchView Answer on Stackoverflow
Solution 30 - GitMike LeeView Answer on Stackoverflow
Solution 31 - GitbruegthView Answer on Stackoverflow
Solution 32 - GitMik RView Answer on Stackoverflow