How to change remote origin url for Git repo

In this tutorial I will show you how you can change your git repo's remote origin url.

Let's get started. I have created a test repo on GitHub to use in order to go through the steps. What I will be simulating in this tutorial is cloning a repo with the https repo url and then changing it to use the ssh url.

The same steps can be used to change an ssh url to an https url, but it would just need to be done in the opposite order.

Cloning the repo:

git clone https://github.com/programmingwithswift/ChangeRemoteOriginTest.git

Once I have cloned the repo, I can run the following command to check the repo's remote url:

git remote -v

When I run that, I get the following output:

-> ChangeRemoteOriginTest git:(master) git remote -v
origin	https://github.com/programmingwithswift/ChangeRemoteOriginTest.git (fetch)
origin	https://github.com/programmingwithswift/ChangeRemoteOriginTest.git (push)

In the above output, you can see that we have the remote name, origin, and the url for the repo which is, https://github.com/programmingwithswift/ChangeRemoteOriginTest.git.

Ok, now let's change the remote origin url to use the ssh url. To do that we need to use the following git command structure, change the arguments to whatever your project needs:

git remote set-url <remote-name> <remote-ssh-url>

In my case I will do the following:

git remote set-url origin git@github.com:programmingwithswift/ChangeRemoteOriginTest.git

Once I have done this, I can run git remote -v again which will output the following:

-> ChangeRemoteOriginTest git:(master) git remote -v
origin	git@github.com:programmingwithswift/ChangeRemoteOriginTest.git (fetch)
origin	git@github.com:programmingwithswift/ChangeRemoteOriginTest.git (push)

As you can see from the above lines, we have successfully changed the https remote url to an ssh remote url. If you have an ssh remote url and want to change it to an https remote url, you can follow the same steps, just make sure to use the https remote url when you run git set-url.