Git
Published in Git
avatar
3 minutes read

Renaming a Local Git Branch

If you want to give your local Git branch a new name, you can easily do so using the following steps:

Check Your Current Branch

Before renaming the branch, it's a good idea to check which branch you are currently on. Open a terminal or command prompt and navigate to your Git project's root directory. Use the following command to view the current branch:

git branch

The branch with an asterisk next to it indicates the branch you are currently on.

Rename the Branch

To rename the branch, you can use the git branch command with the -m option (which stands for "move") followed by the new name you want to give to the branch. For example, to rename a branch named "old-branch" to "new-branch," use the following command:

git branch -m old-branch new-branch

Switch to the Renamed Branch

After renaming the branch, you will still be on the old branch name. To switch to the newly named branch, use the git checkout command:

git checkout new-branch

Alternatively, you can use the shorthand command git switch:

git switch new-branch

Now you are on the renamed branch, and you can continue working as usual.

Verify the Renaming

To ensure that the branch has been successfully renamed, you can use the following command to list all your local branches:

git branch

The branch list will display the new name you provided for the branch.

Be Cautious

When renaming branches, be mindful that you may have branch names referenced in various places like configuration files or other branches. Make sure to update any references to the old branch name to avoid any potential issues.

0 Comment