Git is a distributed version control system that is used to manage multiple versions of code. There are several options and setting in Git that is required and to be setup properly to function as expected.
Git Remote is one of the settings that is an important part of Git. It is a kind of pointer that tells the remote location repository of the code.
Now why we are understanding the Git Remote, Because it plays an important role when we migrate remote repo from one host to another one. So in this case we have to change the remote URL.
Here we will see how to change Git Remote URL.
Changing a Git Remote’s URL
Every Git Repository has Git remote setting, but that should be blank or not set. Usually, we clone the repository from a remote location or URL like GitHub, or GitLab, BitBucket, or your git server in that case
remote is set automatically as origin and pints to the cloned repository. If we have created a new repository on our local, we can setup a new remote and push the code to that new remote repo.
To perform the Git remote URL changes just follow the below steps.
Switch to the directory where your code existed with the repository.
To list existing remotes repositories with their names and URLs simply run the below command:
git remote -v
After issuing the above command you will get output like as below.
origin https://github.com/username/your_repo.git (fetch) origin https://github.com/username/your_repo.git (push)
Now if you want to set a remote URL, use
git remote set-url that will take values first will be the name you want to give that remote URL and the second actual remote URL.
git remote set-url <remote-url-name> <remote-url>
The remote URL works with two protocols those are HTTPS or SSH. You can use any of them and they both are mentioned on your Git Server under your repository.
If you don’t mention any protocol in
remote-url it automatically choose to SSH by default.
HTTPS-based remote repo URL will look like below.
SSH-based remote repo URL will look like as below.
Now as an example, if we need to change the URL the
[email protected]:username/your_repo.git we need to issue this below command:
git remote set-url origin [email protected]:username/your_repo.git
To crosscheck the new remote’s URL if it was successfully updated or not run the below command:
git remote -v
You will get output like below.
origin ssh://[email protected]:username/your_repo.git (fetch) origin ssh://[email protected]:username/your_repo.git (push)
The above command confirms that the remote URL was successfully updated.
You can also verify the changes in the git config file directly. As it also updated the config file, and you can find the changes in
The origin section will look like below after updating the URL.
... [remote "origin"] url = [email protected]:username/your_repo.git fetch = +refs/heads/*:refs/remotes/origin/*
There is a direct method to update the remote URL without running the command, but it’s recommended to use the command to modify that remote URL or change other information. Just for information, you can directly edit
.git/config the file.
Above we have gone through the git tutorial that explains how to change the git remote URL and you guys can find it very easy to follow as there is an only single command
git remote set-url that is explained in the whole above tutorial.
Let us know in the comments if you hit any snag while following the Change Remote URL in the Git tutorial.