How do I rename a git remote?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Renaming a Git remote changes the local shorthand name you use to reference a remote repository URL. The command is git remote rename <old> <new>. This updates all remote-tracking branches (e.g., origin/main becomes upstream/main), configuration entries, and refspecs. Renaming does not affect the remote server — it is purely a local operation. Common reasons include switching from origin to a more descriptive name when working with multiple remotes (e.g., upstream, fork, deploy).
Viewing Current Remotes
Renaming a Remote
All remote-tracking branches update automatically:
Common Workflow: Fork with Upstream
When you fork a repository, you typically want origin pointing to your fork and upstream pointing to the original repository.
Removing and Re-Adding (Alternative)
If renaming causes issues with complex refspecs, you can remove and re-add the remote.
Changing the Remote URL (Not Rename)
If you want to change where a remote points (not its name), use set-url:
Updating Local Branch Tracking
After renaming a remote, local branches that track old remote names need updating.
Common Pitfalls
- Remote name already exists:
git remote rename origin upstreamfails ifupstreamalready exists. Remove or rename the existing remote first:git remote remove upstreamor choose a different name. - Hardcoded remote names in scripts: CI/CD pipelines, deployment scripts, and Makefiles often reference
originby name. After renaming, search your project for hardcoded remote names and update them. Check.github/workflows/,Makefile,deploy.sh, and similar files. - Forgetting to update branch tracking: After renaming, existing local branches may still reference the old remote name in their tracking configuration. Use
git branch --set-upstream-to=<new-remote>/<branch>to update each tracked branch. - Confusing rename with URL change:
git remote renamechanges the local name, not the URL. To change where a remote points, usegit remote set-url <name> <new-url>. These are separate operations. - Losing remote-tracking branches on remove/re-add: Using
git remote removefollowed bygit remote adddeletes all remote-tracking branches for that remote. You mustgit fetchafter re-adding to recreate them. Thegit remote renamecommand preserves tracking branches, so prefer it over remove/add.
Summary
- Use
git remote rename <old> <new>to rename a remote — this updates all tracking branches automatically - Use
git remote -vto verify remote names and URLs - Use
git remote set-urlto change the URL without renaming - Update local branch tracking with
git branch --set-upstream-toif needed after renaming - Prefer
renameoverremove/addto preserve remote-tracking branches
Related reading
- How do I rename a Git repository?
- How do I rename a repository on GitHub?
- How do I resolve git saying "Commit your changes or stash them before you can merge"?
- How do I resolve git saying Commit your changes or stash them before you can merge?
- How do I resolve merge conflicts in a Git repository?
- How do I revert a Git repository to a previous commit?
- How do I revert a merge commit that has already been pushed to remote?
- How do I revert all local changes in Git managed project to previous state?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.