Git Rename Branch: How to Change a Local Branch Name

Estimated read time 3 min read

Introduction to Renaming Git Branches

In Git, branches are an essential part of managing and organizing your codebase. Sometimes, you may need to rename a branch to better reflect its purpose or to follow naming conventions. Renaming a branch in Git is a simple and straightforward process that allows you to update the branch name without affecting its content or commit history. In this article, we will explore different methods to rename a local branch in Git.

Renaming a Branch with the Git Branch Command

The git branch command is a versatile tool that allows you to create, list, and delete branches in Git. It can also be used to rename a local branch. Here’s how you can rename a branch using the git branch command:

  1. Check Out a Different Branch: Before renaming the branch, ensure that you are not currently on the branch you wish to rename. If you are, switch to a different branch using the git checkout command.
  2. Rename the Branch: Run the following command, replacing <old-branch-name> with the current branch name and <new-branch-name> with the desired new name:
git branch -m <old-branch-name> <new-branch-name>

For example, to rename the branch feature-branch to new-feature-branch, you would use:

git branch -m feature-branch new-feature-branch

Note that the -m option stands for “move” and is used to rename the branch.

3. Verify the Renaming: To confirm that the branch has been renamed, you can list all branches using the git branch command:

git branch

The output should display the new branch name alongside other branches.

Renaming the Current Branch with the Git Branch Command

If you want to rename the branch you are currently on, you can use the git branch command with the -m option followed by the new branch name. Here’s how you can rename the current branch:

  1. Verify the Current Branch: Before renaming the branch, confirm that you are on the branch you wish to rename. You can use the git branch command to check the current branch.
  2. Rename the Current Branch: Run the following command, replacing <new-branch-name> with the desired new name:
git branch -m <new-branch-name>

For example, to rename the current branch to new-branch, you would use:

git branch -m new-branch

Note that since the -m option is used without specifying the old branch name, Git assumes you want to rename the current branch.

3. Verify the Renaming: To confirm that the branch has been renamed, you can list all branches using the git branch command:

git branch
  1. The output should display the new branch name alongside other branches.

Renaming a Branch with the Git Checkout and Git Branch Commands

Another method to rename a branch involves using both the git checkout and git branch commands. Here’s the process:

Check Out a Different Branch: Before renaming the branch, ensure that you are not currently on the branch you wish to rename. If you are, switch to a different branch using the git checkout command.

Create a New Branch: Run the following command to create a new branch with the desired name, replacing `<new-branch-name

Mark Stain

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.

You May Also Like

More From Author

+ There are no comments

Add yours