Rename a branch locally with a single command

To rename a branch you are currently working on, use git branch -m followed by the new name. If you are on the branch called old-feature and want to call it new-feature, type:

git branch -m new-feature

That changes the name only on your computer. If you have not pushed this branch to a server yet, you are done. If the branch already exists on a remote server like GitHub or GitLab, you will need to delete the old name there and push the new one — the next section covers that.

You can also rename a branch you are not currently on by typing the old name and new name together: git branch -m old-feature new-feature. This works whether you are on that branch or a different one.

Key Takeaways

  • Rename a branch on your computer with git branch -m new-name if you are on that branch, or git branch -m old-name new-name if you are not.
  • After renaming locally, push the new branch name to your server with git push origin new-name.
  • Delete the old branch name from the server with git push origin --delete old-name so teammates see only the new name.
  • If others are working on the old branch, tell them to run git branch -m old-name new-name and git pull on their computers so their local copy matches.

Push the new name to your server and remove the old one

After you rename a branch locally, the server still has the old name. To send the new name to the server, use git push origin new-feature (replace new-feature with your actual branch name). This creates the branch under its new name on the server.

Then delete the old name from the server so no one accidentally works on it. Type git push origin --delete old-feature. The old branch name disappears from the server, and only the new name remains.

If you are working with a team, send them a message that you renamed the branch. They will need to run the same commands on their computers to update their local copies.

Update your local copy if someone else renamed a branch

If a teammate renamed a branch on the server and you still have the old name on your computer, your local copy is out of sync. First, fetch the latest information from the server: git fetch origin. This downloads what branches exist on the server without changing your current work.

Then rename your local branch to match: git branch -m old-name new-name. If you are currently on that branch, use git branch -m new-name instead. After that, run git pull to make sure you have the latest code under the new branch name.

What happens if you rename the main branch

Renaming the main branch (often called main or master) works the same way technically, but it affects everyone on the team. Before you rename it, talk to your teammates and make sure they know it is coming.

After you rename it locally and push the new name to the server, your teammates will see the old name in their local copies. They should run git branch -m old-name new-name and then git pull to update. Some hosting services like GitHub let you set a default branch in settings, so make sure the server is pointing to the new main branch name.

Undo a branch rename if you made a mistake

If you renamed a branch and have not pushed it to the server yet, straightforward rename it again to the original name: git branch -m wrong-name correct-name. No one else is affected because the change is only on your computer.

If you already pushed the wrong name to the server, rename it locally first, then push the corrected name and delete the wrong one. Type git branch -m wrong-name correct-name, then git push origin correct-name, then git push origin --delete wrong-name. Let your team know so they can update their local copies.

Why you might want to rename a branch

Branch names should describe what you are working on. A branch called fix-login-bug tells teammates what the code does. A branch called test or temp does not. If you created a branch with a vague name or a typo, renaming it makes the project easier to navigate.

Renaming also helps when a project changes direction. If you started a branch for one feature but it turned into something else, the name should reflect the actual work. This keeps your branch list readable and prevents confusion when someone is looking for code related to a specific task.

Frequently Asked Questions

Does renaming a branch lose my code?

No. Renaming only changes the label on the branch. All your commits, files, and history stay exactly the same. The code is safe.

What if I renamed a branch but forgot to delete the old name from the server?

You can delete it anytime with git push origin --delete old-name. It does not matter how long ago you renamed it. The old branch will disappear from the server, and the new name will remain.

Can I rename a branch that other people are currently using?

Technically yes, but you should tell them first. After you rename it on the server, they will see the old name in their local copy until they run git branch -m old-name new-name and git pull. If they push code to the old branch name after you delete it, they will get an error.

Does renaming a branch affect pull requests or code reviews?

Most hosting services like GitHub update pull requests automatically when you rename a branch. The pull request stays open and linked to the new branch name. Check your specific service to be sure, but in most cases the review process continues without interruption.