Git is a powerful version control system that allows developers to track changes to their files or a set of files over time in the codebase and collaborate with others. It offers a wide range of features and operations to perform as it manages small to a very large project. One common operation is renaming a git branch Whether you want to give a new name or modify it according to your needs. The solution is simple, just use The ‘git branch -m’ command and the ‘git push’ command and rename your branch name locally. In this blog, we are going to discuss how you can use these commands to Rename a Branch in Git.
How to rename a local git branch ?
Git makes it easy to rename a branch because of its flexibility and distributed nature. It provides simple and intuitive commands to rename a branch. Let’s know The commands that help to perform the renaming operation.
step 1: Check the current branch
Firstly ensure that you are currently on the branch you want to rename by running the following command.
git branch
step 2: Rename the Branch Locally
To give a new name to the curently active branch(checked), simply run the following command-
git branch -m new-branch-name
To give a new name to the specific branch that is not checked, use the following command-
git branch -m old-branch-name new-branch-name
Replace the “new-branch-name” with the new name you want to give to your branch.
step 3: Push the Renamed Branch to the Remote
To update the remote repository with the new branch, use the following command-
git push origin -u new-branch-name
Step 4: Delete the Old Branch on the Remote
In order to ensure that the old branch is removed from the remote repository, use the following command.
git push origin --delete old-branch-name
Note: If you are using windows or any other case sensitive file system and you would like to rename the branch in upper case, you have to use -M to force the renaming and overwrite any existing branch, Otherwise Git will throw a “branch already exists” error.
git branch -M my-branch MY-BRANCH