You are collaborating on a project with a group of people, and you have defined a naming convention for git branches. You created a new branch, pushed the changes to the remote repository, and realized that your branch name was incorrect.

Luckily, Git allows you to rename the branch very easily using the git branch -m command.

This guide explains how to rename local and remote Git branches.

Renaming Git Branch

Follow the steps below to rename a Local and Remote Git Branch:

  1. Start by switching to the local branch which you want to rename:
    git checkout 
  2. Rename the local branch by typing:
    git branch -m 
  3. If you’ve already pushed the branch to the remote repository delete the remote branch:
    git push origin --delete 
  4. Finally, push the local branch and reset the upstream branch:
    git push origin -u 

That’s it. At this point, you have successfully renamed your local and remote Git branch.

Conclusion

Branches are part of the software development process and one of the most powerful features in Git. Branches are essentially pointers to a certain commit.

Renaming a local Git Branch is a matter of running a single command. However you can’t directly rename a remote branch, you need to delete it and then re-push the renamed local branch.

If you hit a problem or have feedback, leave a comment below.

If you like our content, please consider buying us a coffee.

Thank you for your support!

Sign up to our newsletter and get our latest tutorials and news straight to your mailbox.

Write a comment