Are you working on your on a project which requires you changing the git origin remote to a new URL? or you typed git push and enter which lead to an error saying 403 unable to access.

Here I will explain how to change or switch a git remote origin url.

Git remote is a reference that refers to the copy of the repository that is usually hosted on a git remote server. 

Let’s dive right in,

Change Git Remote URL

All git repository has a git remote that’s linked to them, so whenever you clone a GitHub repository, the origin is set automatically and it’s pointed to the clone git URL you cloned from, but after cloning this repository to your local system, you have the chance to change and add a new remote origin.

This remote origin is always pointed to this common Git hosting platforms like Gitlab, Github and Bitbucket or better still any other private git server you have.

So in order to change the Url of this git remote, you will have to use this git command “git remote set-url” and specify the new remote origin you want it to point too.

So in the folder of the cloned project folder, type the following git command

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

Here is an example;

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

The <remote-name> is the “origin” and the <remote-url> is our new “git repository”

well done, you just fixed the git remote origin error of not being unable to push to a git repository.

Now you might want to crosscheck to see if you are good to go before hitting that git push button.

To verify that the changes were made, you can use this “git remote” command

$ git remote -v

And it will list out the newly set origin URLs like this

OUTPUT

origin https://github.com/user/repo_name.git (fetch)

origin https://github.com/user/repo_name.git (push)

But if you want to change the remote origin via SSH of the git repo, its also allowed, here is how you are going to do it,

You may have configured your Git repository to use SSH key-based authentication.

To 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 to be able to connect.

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

Here’s what the SSH URL looks like :

SSH URL : git@<repo_url>:<url>/<git_repository>.git

Heres example of how the command you gonna type look like;

$ git remote set-url origin git@github.com:user/repository.git

The whole git remote set-url things is going to update the .git/config file, so why don’t we go play this config file and change the git remote url over the git/config file

You can do this with Linux text editors like vim or nano,

nano .git/config, the output is like this and changes things, but it’s advisable to use the git command, its neat, fast and simple.

Conclusion

I believe you have learnt how you can easily change your Git remote origin URL using the “git remote set-url” command.

Go implement it and see the magic.

LEAVE A REPLY

Please enter your comment!
Please enter your name here