Git How to Change Remote Origin (with Examples)

To change your Git remote URL, use the git remote set-url command by specifying:

  1. The name of the remote repo (usually “origin“).
  2. The new remote URL via which the repo will be found in the future.

Here’s what the command looks like:

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

In most cases, your remote name is the origin. So typically running this command would look something like this:

$ git remote set-url origin https://github.com/username/project-name.git

Example

Let’s change the remote URL of an actual GitHub repo to demonstrate how the process works.

I have a (private) Github repo at https://github.com/artturijalli/exampleProject.git which I’ve cloned on my device.

Let’s run git remote -v to see the remote URL.

git remote -v

This reveals what remote URL the local repository tracks at the moment:

Unsurprisingly, the remote origin is set at https://github.com/artturijalli/exampleProject.git. This means whenever I do git pull or git fetch, it retrieves data from that URL.

Now, let’s change the remote URL for the project by using the git remote set-url command.

Notice that the git remote set-url command doesn’t care whether there’s a remote repo with the new URL or not.

For example, I can set the new remote URL to google.com if I want to:

$ git remote set-url origin https://www.google.com

Now the local project is set to track google.com:

Of course, because google.com is not a GitHub repo to which I have access, fetching or pulling will fail…

In this case, the error says it all: “fatal repository not found“. This indicates that you set the remote URL to an incorrect address where there’s no GitHub repo accessible. So make sure to set the URL right!

In my case, I’ll just set the URL back to what it was because that’s the remote origin I want to track.

$ git remote set-url origin https://github.com/artturijalli/exampleProject.git

How to Find the Remote URL on GitHub?

To find out the URL of your remote on GitHub:

  1. Select a repository from your repositories list at https://github.com/username?tab=repositories.
  2. Choose the repo you want to find the URL for.
  3. Now locate the “Clone or Download” button on the repository’s home page.
  4. When you click this button, it shows you the remote URL.

Thanks for reading. Happy coding!

How to Change Git Remote with SSH-Authenticated Repos?

In GitHub, having an SSH authentication to a Git repo is pretty common. To change the remote origin with an SSH authentication, use the same git remote set-url command you saw earlier but provide the URL as an SSH URL to connect.

$ git remote set-url <remote_name> <remote_ssh_url>

For example, changing your remote with SSH might look something like this:

$ git remote set-url origin [email protected]:username/repo.git

Thanks for reading! Happy coding!

Read Also

Git pull vs git fetch