git: how to rename a branch (both local and remote)?

Better Stack Team
Updated on July 25, 2024

To rename a branch in Git, you'll need to follow a series of steps to rename it locally and then update the remote repository to reflect the new branch name. Here’s how you can rename a branch both locally and remotely:

Rename Branch Locally

  1. Switch to the Branch You Want to Rename: First, ensure you are on the branch that you want to rename. Use git checkout to switch to the branch:

     
    git checkout old-branch-name
    
  2. Rename the Branch: Rename the current branch to a new name using git branch -m (short for git branch --move):

     
    git branch -m new-branch-name
    

    Replace new-branch-name with the desired new name for your branch.

Push the Renamed Branch to Remote

  1. Delete the Old Remote Branch: To delete the old branch from the remote repository:

     
    git push origin --delete old-branch-name
    

    Replace old-branch-name with the old name of your branch.

  2. Push the Renamed Branch: Push the renamed branch to the remote repository using the u option to set up the upstream branch:

     
    git push origin -u new-branch-name
    

    This command pushes the renamed branch (new-branch-name) to the remote repository and sets it as the upstream branch so that future git pull and git push commands work without specifying the branch name.

Update Local Tracking Branch (if needed)

If you have other local repositories that have the old branch name as a tracking branch, you might want to update them:

 
git fetch --all --prune

This command fetches all branches from the remote repository and prunes any remote-tracking branches that no longer exist on the remote. This ensures that your local tracking branches are updated.

Example Scenario

Let's say you want to rename old-branch to new-branch:

 
# Switch to the branch you want to rename
git checkout old-branch

# Rename the branch locally
git branch -m new-branch

# Delete the old remote branch
git push origin --delete old-branch

# Push the renamed branch to the remote repository
git push origin -u new-branch

Notes

  • Collaboration: Renaming a branch affects its history and can cause issues for collaborators who have checked out the old branch. Communicate with your team before renaming branches to avoid confusion.
  • History: Renaming a branch does not change its commit history; it only changes the branch name and its references.

By following these steps, you can safely rename a Git branch both locally and on the remote repository, ensuring consistency and clarity in your branch naming conventions.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github