If you are using Git as I do you might need to switch Git remote URLs for multiple reasons i.e. when the remote repo is migrated to another host. In this article, I will explain 2 ways to change Git remote URI. But before discussing this let’s discuss a little bit about Git remote.

Summarized Version

  • Check Git remote URL with
    git remote -v
  • Change Git remote URL by command
    git remote set-url <remote-name> <remote-url>
  • Change Git remote URL by editing
  • Verify Git remote URL changes with
    git remote -v

Checking Git Remote URL

Before starting changing Git remote URL first check Git remote URL you have configured. This will not only help us in identifying remote but we can also verify after changing URL that it has been changed or not. You can check Git remote by executing

git remote -v

It will show you the list of your remotes and their respective fetch and push URLs as below

origin  https://github.com/user/old-repo.git (fetch)
origin  https://github.com/user/old-repo.git (push)
Note: Each Git repository might have 0 or more git remotes. If you have just cloned the repo then you will find the default remote origin which points to the repository you have cloned from.
Note: If you have not added any remote yet which means that you have just initialized your git project you will not find any remote and you will have to use git remote add origin <remote-url>.
In most cases, if you are working with only one host or repository you will find only origin remote but if you are working with multiple remotes note down the remote of which you want to change the URL.

Check Out: Deploying Multiple Git Repositories Using SSH on the Same Server

Changing Git Remote URL by Command (RECOMMENDED)

It is recommended that you change your Git remote URL by command. You can change Git remote URL by running below command

git remote set-url <remote-name> <remote-url>

remote-name: this is the remote you want to change URL of in most of the cases it should be origin.
remote-url: this is the URL you want to change the existing URL with.

Let’s say we want to change origin remote with URL https://github.com/user/new-repo.git the command will look like as below

git remote set-url origin https://github.com/user/new-repo.git

Changing Git Remote URL From Git Config File

Git set-url command updates git config file located at .git/config with the new URL. You can also change git remote URL by directly editing git config file.
If you are using any Linux distro you can simply cd into project directory and edit .git/config by using nano .git/config
If you are using windows edit .git/config with any text editor you like. It will look like as below

[remote "origin"]
url = https://github.com/user/old-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*

Just replace URL with the one you want to change with and save the file.

Verifying Git Remote URL Change

Now for verification again run

git remote -v

You will now see that your remote URL has been changed.

origin  https://github.com/user/new-repo.git (fetch)
origin  https://github.com/user/new-repogit (push)

That’s it. If you face any problems or have any feedback please comment.