2024年7月4日 00:14

How to Rename a Local Git Branch?

When you want to rename a local Git branch, follow these steps:

Assume you are currently on the branch you want to rename. You can use the git branch command with the -m option to rename the branch. The specific command is:

bash
git branch -m new_branch_name

The -m option is a shorthand for --move, which means moving or renaming the branch.

For example, if your current branch is named feature1 and you want to rename it to feature-x, you can run:

bash
git branch -m feature-x

This command renames the current branch feature1 to feature-x.

If you are not on the branch you want to rename, you can use the following command:

bash
git branch -m old_branch_name new_branch_name

For instance, if you are currently on the master branch but want to rename the branch feature1 to feature-x, you can execute:

bash
git branch -m feature1 feature-x

This way, you don't need to switch to the feature1 branch first; you can complete the renaming directly from the current branch.

Renaming branches is a common operation, especially when branch naming was not fully considered initially or when project requirements change. Proper naming makes the purpose of the branch clearer, facilitating team collaboration and management.

标签:Git