THE GITHTML FIELD GUIDE

Add or change a Git remote URL safely

Use git remote add when the remote name does not exist, and git remote set-url when you intentionally want to change an existing remote’s destination. Inspect current fetch and push URLs first. A remote change affects where future network operations go; it does not move your files, rewrite commits, or grant repository access.

Establish the intended destination

Copy the repository’s clone URL from a trusted page and confirm the owner, repository name, and protocol. HTTPS and SSH require different authentication setups. A browser URL for a file or pull request is not the repository clone URL. If the project moved, verify the destination with its owner rather than trusting an unexpected error message or unsolicited link.

Next inspect existing remotes. The error that origin already exists means a name is already configured, not that the local repository is broken. Removing and recreating the remote can discard useful tracking configuration, so prefer a targeted URL update when the alias itself should remain. Do not use this operation to bypass access restrictions.

Choose one configuration operation

The example shows inspection followed by a URL replacement. Run the replacement only after confirming that origin should point to the new address. If origin is absent, use git remote add origin with that verified URL instead. These alternatives are not a sequence to run blindly.

  1. Check whether an explicit push URL remains configured. Updating the fetch URL alone may not change a separately configured push destination.
  2. Test read access with a fetch before considering a push, and inspect the fetched branch names to confirm you reached the expected repository.
shell
git remote -v
git remote get-url --all origin
git remote get-url --push --all origin
git remote set-url origin https://github.com/OWNER/REPOSITORY.git
git remote -v

Review consequences before publication

A changed remote can connect your existing history to a repository that already has independent commits. Do not force-push to make the histories match. Inspect their relationship and choose an integration or migration plan with the repository owner. The URL change itself does not decide which history should survive or which branch should be the default.

If the purpose is only to fetch from an additional repository, a second remote may be clearer than replacing origin. If the purpose is a repository transfer, update any external references through their own workflows; Git configuration does not update issue links, deployment settings, or teammates’ clones. Record the old and new destinations in a safe internal note when the change is operationally significant, then verify the actual push target immediately before sending commits.

A URL correction should be explainable in one sentence, such as a verified repository transfer or protocol change. Unclear motivation is a reason to inspect further before modifying configuration.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All git fundamentals guides →

Read as MarkdownAll guides

Add or change a Git remote URL safely · gitHtml