How to Rename a Git Branch: A Step-by-Step Guide

Renaming a Git branch can be necessary for various reasons, such as correcting a typo, making the branch name more descriptive, or aligning it with project conventions.

Here's a straightforward guide on how to rename a Git branch effectively.

Step 1: Switch to the Branch You Want to Rename

First, you need to switch to the branch that you want to rename. This step ensures that you are renaming the correct branch. Use the checkout command to switch branches:

git checkout <old-branch-name>

Replace <old-branch-name> with the current name of the branch. After executing this command, you should see a message indicating that the switch to the specified branch was successful.

Step 2: Rename the Branch

Once you are on the correct branch, you can rename it using the following command:

git branch -m <new-branch-name>

Here, <new-branch-name> is the new name you want to give to your branch. This command renames the branch you are currently on.

Alternative Single-Line Method

If you prefer, you can rename a branch without switching to it first by specifying both the old and new names in a single command:

git branch -m <old-branch-name> <new-branch-name>

This method is quick and useful if you are sure about the branch name and wish to avoid switching branches.

Step 3: Verify the Change

After renaming the branch, it's a good practice to check the status and ensure that the renaming process was successful. You can view all branches, including the renamed one, using:

git branch -a

This command lists all branches, including local and remote ones, allowing you to confirm the name change.

Subscribe to GIT.WTF!?!

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe