Git branch rename: A Quick Tutorial (2023)

Written by Shan Shah
Updated on January 25, 2023
2 mins read
git rename branch

Naming branches in git is super simple, but many developers get confused about renaming local and remote branches.

This tutorial will cover every aspect of git branch rename scenarios. So you can quickly learn how to rename branches more efficiently.

What are the benefits of renaming a branch in Git?

Git branch rename helps to organize your project better and makes it easier for other contributors to identify the purpose of each branch.

Giving meaningful names to branches makes it much easier for other developers to understand which branches are used for specific features or bug fixes and thus collaborate more efficiently on the project.

It can help streamline the software development process since developers can quickly jump between branches with clear labels describing their purpose.

Git branch rename locally and remotely:

Here you will learn how we can rename the master branch to the main.

You can follow the same approach to rename any local and remote git branch.

How to rename a git branch locally?

Renaming a git branch locally is an important and useful task.

If you want to change the old name of the local git branch to the new name, then you must checkout to the target branch using the following command:

git checkout branch name

Rename Syntax:

git branch -m branch name
git branch -m main
  • -m is a short form of move
  • It will create a new local branch as well.
  • This command will rename a local git branch.

You can check if the branch renamed successfully or not by using the status:

git status

--
On branch main

nothing to commit, working tree clean
--

How to rename a git branch remotely?

To rename the remote master branch, you must push the local main branch to GitHub and delete the old branch.

git push origin -u main 

To delete a branch remotely, you can use the following:

git push origin --delete master

Note: If you see an error [remote rejected]. It means the master branch is already set to the default branch. You will need to change this behavior by going to the settings on GitHub.

Just go to the branch settings of the repository and follow the below steps:

git branch rename conflicts

After that, re-run the above command. This time, the remote master branch should be gone.

That's it! In a nutshell, you learned:

  • How to rename master to the main branch?
  • How to rename git current branch locally and remotely?
  • How to fix remote rejected errors in GitHub?

Project Based Tutorials
Tips & Tricks
Modern Technologies
Real-World Challenges
Creative Content
Build-Apps from Scratch
Web Development Content
17+ Videos Published
Coder Champ ¬© 2023 - 
Privacy Policy
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram