Git branch rename: A Quick Tutorial (2023)

Git branch rename: A Quick Tutorial (2023)

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 some reasons for renaming a branch?

Git branch rename helps to organize your project better and makes it easier for other contributors to identify the purpose of each branch. In addition, giving meaningful names to branches makes it much easier for other developers to understand which branches are used for specific features or bug fixes.

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 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?

 

FAQs:

 

Can we rename existing git branch?

You can rename any existing branch using the "git branch" command with the "-m" flag. For example, this command will re-designate the "old-branch" to "new-branch": git branch -m old-branch new-branch. For this, you don't have to checkout to the specific branch.

Can we rename the GitHub branch name?

Yes, it is possible to alter the label of a GitHub branch. To do this, go to your GitHub repository's "Settings -> Branches" page and select the branch whose name you wish to change. Afterward, input a new title for the branch and press "Update."

 

Leave a comment

All comments are moderated before being published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.