0
0
GitHow-ToBeginner · 3 min read

How to Rename Branch in Git: Simple Commands Explained

To rename a local branch in Git, use git branch -m old-branch-name new-branch-name. To rename the current branch, just use git branch -m new-branch-name. For remote branches, delete the old branch and push the renamed branch.
📐

Syntax

The basic syntax to rename a branch in Git is:

  • git branch -m [old-branch-name] [new-branch-name]: Rename a branch from old-branch-name to new-branch-name.
  • git branch -m [new-branch-name]: Rename the current branch to new-branch-name.
  • For remote branches, after renaming locally, use git push origin :old-branch-name to delete the old remote branch and git push origin new-branch-name to push the renamed branch.
bash
git branch -m old-branch-name new-branch-name

git branch -m new-branch-name

git push origin :old-branch-name

git push origin new-branch-name
💻

Example

This example shows how to rename a local branch named feature1 to feature-updated, then update the remote repository accordingly.

bash
git checkout feature1
# Rename current branch to feature-updated
git branch -m feature-updated

# Delete old branch from remote
git push origin :feature1

# Push new branch to remote
git push origin feature-updated

# Set upstream for new branch
git push --set-upstream origin feature-updated
Output
Switched to branch 'feature1' To https://github.com/user/repo.git - [deleted] feature1 To https://github.com/user/repo.git * [new branch] feature-updated -> feature-updated Branch 'feature-updated' set up to track remote branch 'feature-updated' from 'origin'.
⚠️

Common Pitfalls

  • Trying to rename a branch without switching to it when using git branch -m new-name will rename the current branch, not the intended one.
  • For remote branches, forgetting to delete the old branch on the remote will leave the old branch still visible.
  • Not setting the upstream branch after pushing the renamed branch can cause confusion when pushing or pulling.
bash
git branch -m new-branch-name  # Renames current branch

# Wrong: Trying to rename a different branch without switching
# git branch -m new-branch-name  # This renames current branch, not the target

# Correct way to rename a different branch
# git branch -m old-branch-name new-branch-name
📊

Quick Reference

Summary tips for renaming branches in Git:

  • Use git branch -m to rename local branches.
  • Switch to the branch before renaming if you want to rename the current branch with no old name.
  • Delete the old branch from remote with git push origin :old-branch-name.
  • Push the renamed branch and set upstream with git push --set-upstream origin new-branch-name.

Key Takeaways

Use git branch -m old-name new-name to rename a local branch.
Rename the current branch with git branch -m new-name after switching to it.
Delete the old remote branch with git push origin :old-name after renaming.
Push the renamed branch and set upstream with git push --set-upstream origin new-name.
Always confirm which branch you are on before renaming to avoid mistakes.