To change Git remote URLs, identify the existing remote name, then run git remote set-url with the new address. Most repositories use origin, but you can update any existing remote, such as upstream.
The commands below let you change an HTTPS URL to SSH, replace an outdated repository address, or use different endpoints for fetching and pushing.
Inspect the current remote URL with git remote -v
First, view every configured remote and its fetch and push URLs:
git remote -v
Typical output looks like this:
origin https://github.com/USER/REPO.git (fetch)
origin https://github.com/USER/REPO.git (push)
The first column is the remote name. Use that name in the update command. If you want only the names, run:
git remote
In most cases, you will update origin. If the repository uses another name, replace origin with that name.
Change Git remote origin with git remote set-url origin <URL>
To replace the URL for an existing origin remote, run:
git remote set-url origin <URL>
For example, change the remote to a new HTTPS repository with:
git remote set-url origin https://github.com/USER/NEW-REPO.git
For another existing remote, use its name instead:
git remote set-url upstream https://github.com/USER/REPO.git
set-url changes a remote that already exists. It does not create a new remote. If you need to add a remote that is not configured, use this separate command:
git remote add origin https://github.com/USER/REPO.git
Use git remote add only when the remote name is absent. Running it for an existing origin produces an error instead of replacing the current URL.
Switch between HTTPS and SSH
To switch an existing remote from HTTPS to SSH, set the URL to the SSH form:
git remote set-url origin [email protected]:USER/REPO.git
The equivalent HTTPS form is:
git remote set-url origin https://github.com/USER/REPO.git
Use the HTTPS address if you authenticate through a credential manager or token. Use the SSH address if your SSH key is configured with the Git hosting service. The command changes the stored address; it does not move or rename the repository.
Set Git remote URL, verify fetch and push URLs, and handle missing remotes
By default, Git uses one remote URL for both fetching and pushing. You can set a separate push URL when your workflow fetches from one endpoint but sends changes to another:
git remote set-url origin <FETCH_URL>
git remote set-url –push origin <PUSH_URL>
For example:
git remote set-url origin https://github.com/ORG/REPO.git
git remote set-url –push origin [email protected]:ORG/REPO.git
Check the complete configuration afterward:
git remote -v
For a more targeted check, display all fetch URLs and push URLs separately:
git remote get-url –all origin
git remote get-url –push –all origin
Confirm that the fetch address is the one you intend to read from and that the push address points to the repository where you have write access.
If Git reports fatal: No such remote ‘origin’, inspect the available names with git remote. You may need to use another name, such as upstream, in the set-url command. If no remotes appear, add the first one with git remote add origin <URL>.
