How to change remote git repository
In this article we'll follow simple steps to change a remote Git repository using the command line.
Table of Contents
1. List your existing remotes
To list the existing remotes we open the terminal and type in the following command:
$ git remote -v
If you copied the link to the repository from Clone with SSH in your GitLab, the output should look something like this:
origin This email address is being protected from spambots. You need JavaScript enabled to view it.:user/repository.git (fetch)
origin This email address is being protected from spambots. You need JavaScript enabled to view it.:user/repository.git (push)
If you copied the link to the repository from Clone with HTTPS in your GitLab, the output should look something like this:
origin https://your.git.repo.example.com/user/repository.git (fetch)
origin https://your.git.repo.example.com/user/repository.git (push)
Note: To find the SSH and HTTPS URLs, go to your GitLab, select your project, and click on Clone.
2. Change a remote Git repository
We can change the remote repository by using git remote set-url command:
$ git remote set-url origin This email address is being protected from spambots. You need JavaScript enabled to view it.:user/repository2.git
The command takes two arguments: existing name of the remote (in our case origin) and a new remote URL (in our case This email address is being protected from spambots. You need JavaScript enabled to view it.:user/repository2.git)
In case you change your remote repository to https URL, you will be prompted for your username and password next time you use git fetch, git pull or git push.
If you try to use a link to a non-existing remote, you will get the following error:
> fatal: No such remote 'origin'
When this happens, recheck your URL and make sure it matches the one in your GitLab or GitHub.
If you'd like to learn about Git make sure to check the following articles: