To change your Git remote URL, use the git remote set-url command by specifying:
- The name of the remote repo (usually “origin“).
- 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
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:
- Select a repository from your repositories list at https://github.com/username?tab=repositories.
- Choose the repo you want to find the URL for.
- Now locate the “Clone or Download” button on the repository’s home page.
- 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!