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.
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.
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.
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
git branch -m branch name
git branch -m main
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 --
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:
After that, re-run the above command. This time, the remote master branch should be gone.
That's it! In a nutshell, you learned:
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.
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."