Git: Change Remote Repo URL

While I had initially thought that it's very rare for a remote repository to change location, it actually happens a lot more than I realized. A remote repo may change from one private server to another (like a NAS), from a personal GitHub repo to one in an organization, or maybe even from GitHub to GitLab.

When this happens, you'll need to change the URL for your remote repository in your local repo. In this short article, that's exactly what I'll cover.

First, let's get straight to the command you're here for:

$ git remote set-url <remote-repo> <remote-repo-url>

So if you're wanting to change the URL for "origin" to a new URL on my GitHub account, the command might look something like this:

$ git remote set-url origin https://github.com/scottwrobinson/foobar.git

Then you can verify that the URL has been updated using the remote command with the -v option, which is short for --verbose:

$ git remote -v
origin  https://github.com/scottwrobinson/foobar.git (fetch)
origin  https://github.com/scottwrobinson/foobar.git (push)

Setting Different Repos for Fetch and Push

As you can see in the output from remote -v, there are two lines shown, one pertaining to fetch and one for push. This means you can also set different URLs depending on what command you're running.

An example of this use-case is if you've forked someone's repo and want to continue to pull in updates from them, but then you only want to push changes to your forked version. Here is how you'd handle that:

$ git remote set-url <remote-repo> <original-repo-url>
$ git remote set-url --push <remote-repo> <forked-repo-url>

Notice the second command uses the --push option, which tells Git to only set that URL for push commands. In practice the command execution would look something like this:

$ git remote set-url origin https://github.com/example/foobar.git
$ git remote set-url --push origin https://github.com/scottwrobinson/foobar.git

Notice that the remote repo is named "origin" for both, and just the URL is different. In this example, https://github.com/scottwrobinson/foobar.git is the forked version.

Again, by running the remote -v command we can verify that the URL has been successfully changed:

$ git remote -v
origin  https://github.com/example/foobar.git (fetch)
origin  https://github.com/scottwrobinson/foobar.git (push)

Using Different Protocols

One other thing I wanted to point out is that in all of the examples above I only showed HTTPS URLs, although any valid Git URL can be used. Git supports a large number of URL types, a few of which you can find here:

ssh://[email protected]/path/to/repo.git
git://host.com/path/to/repo.git
https://host.com/path/to/repo.git
file:///path/to/repo.git

So if for any reason you want to pull from a remote repo on GitHub, but then only push your changes to another repo accessible from within your file system, then Git is flexible enough for you to do that:

$ git remote set-url origin [email protected]:example/foobar.git
$ git remote set-url --push origin file://~/projects/foobar.git