How to change Git Remote Origin URL


Introduction

With git remote set-url, you can change the URL of a Git remote repository. Use this command to change the remote URL of the repository you want to change. This command accepts two arguments: the name of the remote repository and the URL of the new repository.

Do you have any experience changing the name of a remote Git repository? Do you intend to move a remote repository into a different location? The URL of a Git repository will be changed by both of these operations. Your remote repository will become unreachable if this occurs.

There is no need to worry! Set the remote URL with the git remote set-URL command. Remote repository URLs can be changed with this command.

In this article, I will explain what git remotes are and how to alter them in an easy and efficient way

What is Git Remote

With git remote, you can set up connections with other repositories, view them, or delete them. A remote connection is more like a bookmark than a direct link to another repository. These names serve as convenient references to URLs that are not so convenient to use in real-time.

This diagram shows two remote connections between your repository, a central repository, and another developer's repository. The origin can be passed to another developer so that they can shortcut to other Git commands. The connection URLs won't have to be referenced by full URLs as a result.


Repositories must be updated with new URLs when they are renamed or moved to another hosting platform.

What is origin in Git?

Using git remote -v in your repository will probably show you something called the origin. In many Git messages, you will see the origin. It is the name given to the URL of the remote repository in human-friendly language. The default is the origin, and it's like a key-value pair.

What isan Upstream Branch in Git

Tracking a remote branch via a local branch is called an upstream branch. Local branches are called upstream of remote branches. In addition, the local branch can pull all changes from this remote branch and push them to the upstream branch

How to list your existing remotes

List the existing remotes by typing the following command in the terminal −

$ git remote -v

In your GitHub, you should see something similar to this if you cloned the repository using SSH −

origin git@github.com:muthuannamalai12/tutorialspoint.git (fetch)
origin git@github.com:muthuannamalai12/tutorialspoint.git (push)

The following is what you should see if you copy the repository link from Clone with HTTPS into GitHub −

origin https://your.github.com/muthuannamalai12/tutorialspoint.git (fetch)
origin https://your.github.com/muthuannamalai12/tutorialspoint.git (push)

How to change a remote Git repository URL

The git remote set-url command can be used to change the remote repository −

$ git remote set-url origin git@github.com:muthuannamalai12/tutorialspoint.git

There are two arguments in the command: the name of the remote that already exists (in our case origin) as well as a new remote URL (in our case git@github.com:muthuannamalai12/tutorialspoint.git).

Once you have changed your remote repository URL to https, you will be asked to enter your username and password to use git fetch, git pull, or git push.

The following error will appear if you attempt to use a non-existing remote −

> fatal: No such remote 'origin'

Be sure that your URL matches the one in your GitHub account when you encounter this problem.

How to Change Git Remote URL using SSH

Git repository authentication can be configured to use SSH keys in some cases. If that is the case, you can change the remote URL by using the command git remote set-url

The “git remote set-url” command works similarly if you want to change your Git origin using SSH authentication, but you need to provide the SSH URL.

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

In most cases, the SSH URL looks like this −

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

In the case of a Github repository, for example, you would use the following command to change the remote.

$ git remote set-url origin git@github.com:muthuannamalai12/tutorialspoint.git

Conclusion

In this article, you learned how to easily change the Git remote URL (also known as origin) by using the command "git remote set-url". In addition, you learned that you can change it via a password-protected SSH connection.

Updated on: 11-Oct-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements