Home Software Engineering How To Change Git Remote Origin

How To Change Git Remote Origin

by schkn

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.

Change Git Remote URL

In order to change the URL of a Git remote, you have to use the “git remote set-url” command and specify the name of the remote as well as the new remote URL to be changed.

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

For example, let’s say that you want to change the URL of your 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 : git@<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!

You may also like

4 comments

Alexander September 1, 2021 - 8:45 am

Thanks

Reply
Abraham May 16, 2022 - 11:53 pm

Hi. the example code you give does not match the description.
You say
“In order to change the URL of a Git remote, you have to use the “git remote set-url” command and specify the name of the remote as well as the new remote URL to be changed.”

but your code shows:

$ git remote set-url

Where is the new remote URL?

Reply
Mursid September 8, 2022 - 4:36 pm

Thanks a lot buddy

Reply
Francis December 27, 2022 - 3:56 pm

That was breathtakingly easy. Thankyou.
I imagined I had to delete the old, and setup the new.

But 2 commands, the above, and ‘git push’ and my old server that’s going down today was history!

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.