How to rename a branch in git

Renaming the local branch

We can rename the current local branch using the -m option with the git branch command:

# If you are in the same branch
git branch -m "new_name"

To rename a local branch that is not the currently checked-out branch, we can use:

# If you are in the different branch
git checkout -m "old_name" "new_name"

Renaming the remote branch

To rename a remote branch we need to:

  • delete the remote branch
  • push the renamed brach
# delete the old branch:
git push origin --delete "old_name"
# - [deleted] old_name

# push the renamed branch
git push -u origin "new_name"

If your teammates are working on the renamed branch, then they need to do the following operations:

# Switch to the "renamed" branch:
git checkout "old_name"

# Rename it:
git branch -m "new_name"

# Fetch latest changes 
git fetch

# Remove the existing tracking connection with "origin/old_name":
git branch --unset-upstream

# Create a new tracking connection with the new "origin/new_name" branch:
git branch -u origin/new_name

In the commands above, we have moved to the renamed branch, renamed it, and changed the upstream.

Free Resources