02Home & Garden · Updated
How to Change a Branch Name in Git
Short answer
Rename a local Git branch with git branch -m
Use git branch -m to rename the branch you are on, or add the old and new names to rename another local branch. If the branch is shared, update the remote and tell collaborators.
Renaming a Git branch changes its name in your local repository; it does not rename the branch on a remote such as origin. Before changing a shared branch, coordinate with anyone using it, since their local branches and open work may still refer to the old name. You need Git installed and access to the repository. The commands below work in a terminal opened inside that repository.
What You’ll Need
- A local Git repository, with the changes you want to keep committed or safely saved.
- A terminal and permission to rename the local branch. Updating a remote also requires permission to push and delete branches there.
- The branch’s exact current name and the new name you want. Branch names are case-sensitive in many Git environments, so copy them carefully.
Step-by-Step
- Open the repository. In a terminal, change to the project directory. Run
git statusto confirm Git recognizes the repository and check whether you have uncommitted changes. Commit or otherwise safeguard work you do not want to risk. Renaming a branch does not normally discard changes, but a clean worktree makes the operation easier to verify. - Check which branch is active. Run
git branch --show-current. The output is the current branch name. You can also rungit branch; the active branch is marked with an asterisk. If the command shows no branch name, you may be in a detached HEAD state, where a commit is checked out directly rather than through a branch. Switch to or create a branch before using the current-branch rename command. - Choose the rename command. If you are currently on the branch you want to rename, run
git branch -m new-name, replacingnew-namewith the desired name. The-moption means move or rename. If you are on a different branch, usegit branch -m old-name new-name. For example,git branch -m feature/login feature/sign-inrenames the local branch without checking it out first. - Verify the local name. Run
git branch --show-currentif you renamed the active branch, orgit branchto list local branches. Confirm the new name appears and the old one does not. A rename affects the branch reference in this repository; it does not rewrite commits or change the contents of your project. - Update the remote if needed. If the old branch was already published and you want the new name on the remote, push the new branch and set its upstream with
git push -u origin new-name. Replaceoriginif your remote has a different name; check remotes withgit remote -v. The upstream setting connects future pushes and pulls on your local branch to the remote branch. - Remove the old remote branch only after checking. Once the new remote branch is visible and collaborators are ready, remove the old remote name with
git push origin --delete old-name. Do not do this if the old branch must remain available, if the new branch has not been pushed successfully, or if your team has not agreed to the change. Deleting a remote branch can disrupt pull requests, automation, or other people’s work. - Tell collaborators what changed. Share the new branch name and ask collaborators to update their local references. Git hosting services may update some pull request links or show a redirect, but do not assume every integration, script, deployment rule, or bookmark will follow the rename automatically.
Common Mistakes
- Assuming a local rename updates the remote. It does not. Push the new name separately, and delete the old remote branch only when it is safe to do so.
- Using the wrong command form.
git branch -m new-namerenames the current branch. To rename a branch while on another one, include both names:git branch -m old-name new-name. - Deleting the old remote branch too soon. First push and verify the new remote branch. Check whether open pull requests, teammates, or automation still depend on the old one.
- Forgetting references outside Git. CI jobs, deployment settings, documentation, scripts, and pull request targets may contain the old name. Search and update relevant project configuration.
- Confusing a branch rename with a commit change. Renaming changes the label pointing to the branch’s commit history; it does not amend commits or merge branches.
Tips
- Use
git branch --listto see local branches andgit branch -rto see remote-tracking branches. These lists represent different references. - Use
git status -sbafter renaming to check the current branch and whether it tracks a remote branch. - For a shared branch, agree on the timing and communicate the exact old and new names before changing the remote. This prevents collaborators from continuing to push to an unexpected branch.
- Use a descriptive, consistent name that fits the repository’s conventions. Avoid spaces and check the name carefully before sharing it.
Troubleshooting
Git says the branch does not exist: Check spelling and capitalization with git branch. A remote-only branch may appear as origin/old-name; a local rename command needs a local branch. You can create a local tracking branch with git switch --track origin/old-name, then rename it.
The new name is already in use: Git will not replace an existing branch name with a simple rename. List branches, confirm which reference you intend to keep, and choose a distinct name or carefully resolve the conflict before trying again.
Push is rejected: Confirm the remote name with git remote -v, check that you have permission, and verify that the new branch name is correct. If the remote branch already exists, inspect it before pushing; avoid force-pushing unless you understand the effects and your team has agreed.
The renamed branch no longer tracks its upstream: Set it explicitly with git push -u origin new-name. If you are not ready to publish it, continue working locally and configure tracking later.
A teammate still sees the old branch: Their local repository is separate. They should fetch the latest references, switch to the new branch, and remove their obsolete local branch only after confirming they no longer need it. Coordinate before anyone deletes or force-updates shared work.
Frequently Asked Questions
Can I rename the branch I am currently on?
Yes. Run git branch -m new-name. Verify the result with git branch --show-current.
Does renaming a branch change its commits?
No. A branch name is a reference to a commit and its history. Renaming the reference does not change the commits.
Does git branch -m rename the branch on GitHub or another remote?
No. It renames only the local branch. Push the new name to the remote, then decide whether the old remote branch can be deleted.
What is the difference between -m and -M?
-m renames a branch and refuses to overwrite an existing branch with the destination name. Uppercase -M forces the rename by allowing that name to be replaced, so use it only after checking the existing branch and confirming you intend to overwrite it.
Can I rename a branch with uncommitted changes?
Usually, yes: renaming the current branch does not itself discard working-tree changes. Still, check git status first and save important work so you can distinguish branch-related issues from unrelated edits.
Do collaborators need to rename their local branches?
If they use the branch, they need to switch their local work to the new remote name and update tracking as appropriate. Share the change first, especially if the branch is used by pull requests or automation.
Related
- How to Read a John Deere Serial Number
- How Bees Get Pollen From Flowers
- How Clouds Become Electrically Charged
Further reading: Git. Details can change, so check the official page for anything that matters to you. Spotted a mistake? Tell us. How we write an answer.