Signup/Sign In

How to Change Git Remote Origin(URL)

In Git terminology, a remote is short for a remote repository. We can set up connections with the remote repositories and can push or pull from these repositories. Origin is the default name given to the original repository from which we cloned our local repository. It is a central remote repository where all the collaborators can push their changes and pull the changes made by other developers.

Sometimes we may need to migrate our remote repository to a different host or merge it with other existing repositories. In such cases, the URL of our remote repository changes and we have to make the necessary changes in our local repository to make sure that the remote connection is stabilized and we are working with the correct repository. Let's learn how to do this.

Local Repositories connected to remote repository

Changing Git Remote Origin

  • Git provides us the Git Remote command to work with remote repositories and set up remote connections. We will be using this command along with the set-url option to change the URL of our remote.
  • Before changing the URL, we can check the remote by running the Git Remote command with the -v or the --verbose option. This displays the remote name and the URL to which we are pushing and from where we are fetching changes.
$ git remote --verbose
  • Let's use the Git Remote command with the set-url option to change the URL of our remote. Since we are working with the origin, the remote name can be replaced with origin. Origin is a classic default name given to a remote.
$ git remote set-url <remote-name> <new-remote-URL>
  • The remote URL can be found on the repository page. For GitHub, this URL can be found on the repository page by clicking on the "Code" button. This URL can start with HTTPS or SSH.

Getting the repository URL from GitHub

Let's try to understand the above process with the help of an example.

  • Consider that we have migrated our remote repository from GitHub to BitBucket. Now we want to change our remote URL in the local repository. First, run the Git Remote command with the -v(verbose) to see the current URL.

Running the Git Remote command with --verbose to check the current URL

  • Now head over to your repository on Bitbucket and copy the repository URL. The URL can be found by clicking on the "Clone" button in the top right corner of the repository page.

Getting the new repository's URL from Bitbucket

  • Run the Git Remote command with the set-url option and enter the copied URL.

Changing the URL using the set-url option with Git Remote

  • Make sure that everything was set up correctly by using the --verbose option.

Checking the URLs to make sure that they were properly modified

Summary

We will be working with remote repositories quite frequently and sometimes we may merge different repositories or migrate them to some other Git hosting platform. In these cases, it is important to reset our remote in the local repository so that it has the new and updated URLs. This is done by using the Git Remote command with the set-url option. The set-url option updates the .git/config file with the new URL. This can be manually by editing this file but it is not recommended as we may end up corrupting our files.