Did you change your remote git repository name? Do you want to move a remote repository to another location? Both these operations will make you push to your remote origin on a daily basis.
You can be rescued from such cases by using the git commands that suit the related concept here git remote set-url command will help you most. It permits you to change the URL of a remote repository. In this tutorial, we have explained what is git remote and how to change git remote origin easily?
- What is a Git Remote?
- Change Git Remote URL
- Changing Git Remote to SSH
- Getting Git Remote URL on GitHub
A Git remote is a pointer that links your local version of a repository to a remote repository. A Git repository can hold many remotes linked to it and mostly they have only one remote. Repositories with more than one remote are normally linked to various development environments like testing, staging, or production.
When you modify the repository name or move it to another hosting platform, you’ll require to update your remote URLs. Let’s look at the process of changing the Git Remote URL from the below modules.
- How To Clone a Git Repository | Clone a Git Repository with Command Line & Sourcetree
- How To Set Upstream Branch on Git | What is a Git Upstream Branch? | Git Set Upstream Branch without Push
- How To Add and Update Git Submodules | Definition of Submodule
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
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
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@example.com:user/repository.git
If you are having trouble identifying the URL of your Git remote on Github, the next section might be helpful.
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.
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.
Also Check: How To Clone a Git Repository
When clicking on it, you should be presented with the URL of your Git repository.
You can now use the “git remote set-url” command in order to set your Git remote URL properly (using the previous section).
In this tutorial, you learned how you can easily change your Git remote URL (commonly named origin) by using the “git remote set-url” command.
You also learned 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!