How To Change Git Remote Origin

As a developer, you are probably pushing your code to your remote origin every day.

Git is a decentralized versioning system : even if you make changes locally, you have to push them to the central repository on a regular basis.

However, in some cases, you might choose to migrate your Git repository or to merge existing ones.

As a consequence, you may need to change the Git origin remote to a new URL.

In this tutorial, you are going to learn how you can change the URL of a Git remote easily.

Table of Contents

Git origin remote.

In order to achieve that, you would use the “set-url” command on the “origin” remote and you would specify the new URL.

$ git remote set-url origin https://git-repo/new-repository.git
git change remote using set url

Congratulations, you successfully changed the URL of your Git remote!

In order to verify that the changes were made, you can use the “git remote” command with the “-v” option (for verbose)

$ git remote -v
list remote using git remote

Changing Git Remote to SSH

In some cases, you may have configured your Git repository to use SSH key-based authentication.

If you want to change your Git origin remote using SSH authentication, you can use the same “git remote set-url” command but you will have to use the SSH URL in order to connect.

$ git remote set-url <remote_name> <ssh_remote_url>

The SSH URL usually takes the following form :

SSH URL : [email protected]<repo_url>:<url>/<git_repository>.git

For example, if your repository was configured on Github, you would use the following command to change your remote.

$ git remote set-url origin [email protected]:user/repository.git
change remote url to ssh repository

If you are having trouble identifying the URL of your Git remote on Github, the next section might be helpful.

Getting Git Remote URL on GitHub

If you need to quickly find the URL of your Git remote on Github, you first need to select your repository by navigating to your repository list.

> https://github.com/<user>/repositories
repository list on github

Under your repository list, select the repository you are interested in.

Now that your repository is select, locate the “Clone or Download” option on the right-corner of your screen.

Note : looking to learn how you can easily clone a Git repository? We got a complete guide written for you!

When clicking on it, you should be presented with the URL of your Git repository.

clone repository on github

You can now use the “git remote set-url” command in order to set your Git remote URL properly (using the previous section).

Conclusion

In this tutorial, you learnt how you can easily change your Git remote URL (commonly named origin) by using the “git remote set-url” command.

You also learnt that you can change it using a password protected SSH address.

If you are looking for an easy way to generate SSH keys for Git, you should take a look at our tutorial.

SSH key authorization is preferred over password authentication, if you have a repository with a large activity.

If you are interested in Git or in Software Engineering, we have a complete guide dedicated to it on the website, so make sure to check it out!

Posted by Contributor