You can change a Git remote URL using the git remote set-url command. Navigate to the repository whose remote URL you want to change and then execute this command. The set-url command accepts two arguments: the remote name and the new repository URL.
Have you changed the name of a remote Git repository? Are you moving a remote repository to another location? Both of these operations will change the URL of a Git repository. This will cause any references to your remote repository to break.
Do not worry! The git remote set-url command is here to the rescue. This command allows you to change the URL of a remote repository.
In this guide, we’re going to talk about what git remotes are and how you can change a git remote. We’ll walk through an example to help you get started.
What is a Git Remote?
A Git remote is a pointer that links your local version of a repository to a remote repository.
Git is a distributed version control system. This means that multiple developers can keep their own copies of a project on their own machines. The changes that you make to a repository will only be accessible by other developers when you push them to a remote server.
A Git repository can have multiple remotes linked to it. Most repositories only have one remote. Repositories with more than one remote are usually linked to different development environments such as testing, staging, or production.
When you change the name of a repository or move it to another hosting platform, you’ll need to update your remote URLs.
How to Change a Git Remote
The git remote set-url command changes the Git remote associated with a repository. This command accepts the name of the remote (which is usually “origin”) and the new remote URL to which you want the repository to point.
Let’s start by navigating into a repository:
Now that we’re in a Git repository, we can start changing its remotes. We’re going to check our existing remotes to see what has been set using git remote -v:
git remote -v
This command returns:
origin https://github.com/Career-Karma-Tutorials/git-submodule-tutorial (fetch)
origin https://github.com/Career-Karma-Tutorials/git-submodule-tutorial (push)
We have one remote called “origin”. This remote is used to both fetch code from and push code to a remote repository. You should see a similar output when you run this command unless you have multiple remotes set for a project.
We’re going to change the remote of this repository to git-submodule. This is because we have renamed our repository on Github. You can change a remote using the git remote set-url command:
git remote set-url origin https://github.com/Career-Karma-Tutorials/git-submodule
“origin” refers to the name of the remote whose URL we want to change. The URL we have specified is the new URL for the project.
You can specify either a HTTP or SSH URL as a remote. For instance, we could change our link to an SSH URL like this:
git remote set-url origin firstname.lastname@example.org:Career-Karma-Tutorials/git-submodule.git
This will point the “origin” remote to an SSH URL.
We can verify the new remote URL using the git remote -v command:
git remote -v origin
Our remotes have been changed:
origin email@example.com:Career-Karma-Tutorials/git-submodule.git (fetch)
origin firstname.lastname@example.org:Career-Karma-Tutorials/git-submodule.git (push)
We’ve done it!
Change a Remote Manually
You can change a remote manually by modifying a Git repository’s config file inside your working directory. This approach is practical if you are going to make multiple changes to the configuration of a Git repository.
Open up the file .git/config in your Git repository. Then, scroll down until you reach the [remote “origin”] line:
[remote "origin"] url = email@example.com:Career-Karma-Tutorials/git-submodule.git fetch = +refs/heads/*:refs/remotes/origin/*
We can change this code to modify the “origin” remote. Once you have made any changes you need to make, you can save the file.
It is best to change a remote using the Git command. This is because there’s a higher risk that you make a mistake in your configuration file if you alter it manually.
fatal: No such remote ‘[name]’
You may encounter an error
fatal: No such remote ‘[name]’ when you try to change the remote of a repository:
fatal: No such remote ‘[name]’
This error occurs when you try to change the URL of a remote that does not exist. To solve this error, make sure you have correctly typed in the name of the remote whose URL you want to change.
You can change a Git repository’s remote URL using the git remote set-url command. You can also modify a remote URL by modifying the .git/config file in a repository.
Now you’re ready to start changing remotes using Git like an expert!
To learn more about Git, read our complete How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more