How Do I Rename a Local Git Branch

Git is a popular version control system that helps developers keep track of changes to their codebase.

In Git, branches are used to work on different features or fixes simultaneously without affecting the main code.

If you’ve been working on a Git branch for a while, you might find that you need to rename it for better organization or to reflect changes in the feature.

Here’s a step-by-step guide on how to rename a local Git branch.


Step 1: Check the Current Branch

Before you start renaming your branch, make sure you are in the right branch by using the following command:

$ git branch

The branch you are currently on will have an asterisk (*) next to it.

Step 2: Switch to a Different Branch

If you are on the branch you want to rename, switch to a different branch by using the following command:

$ git checkout branch_name

Replace “branch_name” with the name of the branch you want to switch to.

Step 3: Rename the Branch

To rename a branch, use the following command:

$ git branch -m new_branch_name

Replace “new_branch_name” with the name you want to give to your branch. This command will rename your current branch.

Step 4: Verify the Renamed Branch

To verify that your branch has been renamed, use the following command:

$ git branch

This command will show you a list of all the branches in your repository. The branch you just renamed should now have the new name.

Step 5: Push the Changes to Remote

If you have a remote repository set up, you’ll need to push the changes to the remote repository by using the following command:

$ git push origin :old_branch_name new_branch_name

Replace “old_branch_name” with the old name of your branch and “new_branch_name” with the new name.

This command will delete the old branch from the remote repository and push the renamed branch to the remote repository.

That’s it! You have successfully renamed a local Git branch.

Renaming a branch can be useful when you want to better organize your code or to reflect changes in the feature you are working on.

Just remember to switch to a different branch before renaming your current branch and to push the changes to the remote repository if you have one set up.