How to Change a Git Remote's URL
Updated on
•6 min read

Git remote is a pointer that refers to another copy of the repository that is usually hosted on a remote server.
In some situations, like when the remote repository is migrated to another host, you need to change the remote’s URL.
This guide explains how to change the URL of a Git remote.
Changing a Git Remote’s URL
Each Git repository can have zero or more Git remotes linked to it. When you clone a repository, the name of the remote is set automatically to origin and points to the repository that you cloned from. If you created the repository locally, you can add a new remote .
The remote can point to a repository hosted on a Git hosting service such as GitHub, GitLab, and BitBucket or your private Git server .
Follow the steps below to change the URL of a remote:
Change to the directory where the repository is located:
cd /path/to/repositoryRun
git remoteto list the existing remotes and see their names and URLs:git remote -vThe output will look something like this:
origin https://github.com/user/repo_name.git (fetch) origin https://github.com/user/repo_name.git (push)Use the
git remote set-urlcommand followed by the remote name, and the remote’s URL:git remote set-url <remote-name> <remote-url>The remote’s URL can start with HTTPS or SSH, depending on the protocol you’re using. If no protocol is specified, it defaults to SSH. The URL can be found on the repository page of your Git hosting service.
If you’re changing to HTTPS, the URL will look something like:
https://gitserver.com/user/repo_name.gitIf you’re changing to SSH, the URL will look like:
[email protected]:user/repo_name.gitFor example, to change the URL of the
originto[email protected]:user/repo_name.gityou would type:git remote set-url origin [email protected]:user/repo_name.gitVerify that the remote’s URL was successfully changed by listing the remote connections:
git remote -vThe output should look like this:
origin ssh://[email protected]:user/repo_name.git (fetch) origin ssh://[email protected]:user/repo_name.git (push)
That’s it. You have successfully changed the URL of the remote.
What the git remote set-url command does is update the repository .git/config file with a new URL to the remote repository.
...
[remote "origin"]
        url = [email protected]:user/repo_name.git
        fetch = +refs/heads/*:refs/remotes/origin/*
You can also change the remote’s URL by editing the .git/config file with a text editor
. However, it is recommended to use the git command.
Conclusion
Changing a Git remote’s URL is as simple as running: git remote set-url <remote-name> <remote-url>.
If you hit a problem or have feedback, leave a comment below.


