How to Change Git Remote URL
Introduction
Before we begin talking about how to change Git remote URL, let’s briefly understand - What is Git Remote URL?
Git remote is a pointer to a different copy of the repository that is usually hosted on a different server.
You may need to alter the remote repository's URL in specific circumstances, such as when it is relocated to a new server.
In this tutorial, you will change the remote Git URL. We will also address a few FAQs on how to change Git remote URL.
Change Git's Remote URL
There can be zero or more Git remotes associated with each Git repository. When you clone a repository, the remote's name is set to origin and points to the repository from which you cloned. You can add a new remote if you created the repository locally.
The remote can point to a repository hosted on GitHub, GitLab, or BitBucket, as well as your own private Git server.
To update a remote's URL, follow the procedures below:
1) To get to the repository, change to the following directory:
cd /path/to/repository
2) To see the names and URLs of the existing remotes, run git remote:
git remote -v
3) You will get an output like below:
Output
origin https://github.com/user/repo_name.git (fetch)
origin https://github.com/user/repo_name.git (push)
4) Use the git remote set-url
command, which should be followed by the remote name and the URL:
git remote set-url <remote-name> <remote-url>
Depending on the protocol you're using, the remote's URL can begin with HTTPS or SSH. If no protocol is specified, SSH is used by default. The URL can be located on your Git hosting service's repository page.
The URL will look like this if you're switching to HTTPS:
https://gitserver.com/user/repo_name.git
If you're switching to SSH, the URL will be as follows:
git@gitserver.com:user/repo_name.git
You would type: git@gitserver.com:user/repo_name.git
to modify the origin URL:
git remote set-url origin git@gitserver.com:user/repo_name.git
5) List the remote connections to ensure that the remote's URL was correctly changed:
git remote -v
This is what the final output should look like:
Output
origin ssh://git@gitserver.com:user/repo_name.git (fetch)
origin ssh://git@gitserver.com:user/repo_name.git (push)
That is all there is to it. You've successfully modified the remote's URL.
The git remote set-url
command changes the URL to the remote repository in the repository's .git/config
file.
...
[remote "origin"]
url = git@gitserver.com:user/repo_name.git
fetch = +refs/heads/*:refs/remotes/origin/*
You can also edit the .git/config
file with a text editor to alter the remote's URL. It is, however, advised that you use the git command.
FAQs to Change Git Remote URL
How can I check the current Git remote URL?
You can check the current Git remote URL by using the git remote -v
command. It will display the remote name and its associated URL.
Can I change the remote URL for multiple remotes at once?
Yes, you can change the remote URL for multiple remotes at once by specifying each remote name separately with the git remote set-url
command.
What happens to my existing commits when I change the Git remote URL?
Changing the Git remote URL does not impact your existing commits. It only affects where your future commits will be pushed.
Can I change the remote URL to a different Git hosting provider?
Yes, you can change the remote URL to a different Git hosting provider, as long as it supports the Git protocol.
Are there any prerequisites for changing the Git remote URL?
To change the Git remote URL, you should have proper access to the repository and be authenticated with the necessary credentials.
Will changing the remote URL affect other collaborators?
Changing the Git remote URL only affects your local configuration, so it will not impact other collaborators unless they explicitly choose to update their own remote URLs.
Can I revert back to the old Git remote URL?
Yes, you can revert back to the old Git remote URL by using the git remote set-url
command and providing the previous URL.
Conclusion
It's as simple as running: git remote set-url <remote-name> <remot-url>
to change the URL of a Git remote.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.