Change the URL
git remote set-url origin https://github.com/USER/NEW-REPO.git
origin is the name Git gives the remote you cloned from. Replace USER/NEW-REPO with the new owner and repository name. Typical reasons: the repository was renamed or moved, or you want to switch from HTTPS to SSH.
See the Current Remotes
git remote -v
Each remote appears twice, once with (fetch) and once with (push). For the address of one remote only:
git remote get-url origin
Check That the New Address Works
git remote -v
git ls-remote origin
git fetch origin
git ls-remote lists the branches on the remote without changing anything in your clone. If it prints branch names, the address and your access are correct.
Switch from HTTPS to SSH
Open the repository page on your Git hosting service and copy the SSH address shown there; it starts with the user name git and ends in .git. Then set it:
git remote set-url origin SSH_ADDRESS
Replace SSH_ADDRESS with the copied address. SSH needs a key on the server that the hosting service knows; the steps are in how to use SSH keys with GitHub. If you set up a host alias there, the address takes the form github-shop:USER/REPO.git, with the alias in place of the host name.
To go back to HTTPS, set the HTTPS address the same way. GitHub no longer accepts your account password for Git over HTTPS: when Git asks for a password, enter a personal access token.
Add a Second Remote
A fork usually tracks the original project as upstream:
git remote add upstream https://github.com/ORIGINAL-OWNER/REPO.git
git fetch upstream
Branches from it appear as upstream/main and so on. Read git pull vs git fetch for how to bring those changes into your branch.
Rename or Remove a Remote
git remote rename origin old-origin
git remote remove old-origin
Removing a remote deletes its settings and its remote-tracking branches from your clone. It does not delete the repository on the hosting service, and your local branches and commits stay. Local branches that tracked the removed remote lose their upstream setting; once the new remote is added and fetched, set it again with git branch -u origin/main.
Use a Different Address for Push
git remote set-url --push origin SSH_ADDRESS
Fetches still use the first address; pushes go to the second. git remote -v shows both. Both addresses must point to the same repository, for example fetching over HTTPS and pushing over SSH. To push to a different repository, add it as a separate remote.
Edit the Config File
Remotes live in .git/config in a section such as:
[remote "origin"]
url = https://github.com/USER/REPO.git
fetch = +refs/heads/*:refs/remotes/origin/*
Editing the url line has the same effect as set-url. The command is safer, because it cannot break the file's syntax.
Common Errors
- Repository not found: a typo in the address, or your account has no access to a private repository.
- Permission denied (publickey): the SSH key is missing or not added to the hosting service.
- No such remote: the name is wrong; check it with
git remote -v, or create it withgit remote add. - Authentication failed over HTTPS: a password was used where a token is required.
If Git itself is missing on the server, see how to install Git on Linux.