How to Rename current branch in GIT


To rename the current branch in Git, you can use the git branch command with the -m or --move option, followed by the new name you want to give to the branch.

Here’s an example of how to rename the current branch:

  1. First, make sure you are on the branch you want to rename. You can use the git branch command to check the current branch:
$ git branch
* main
  feature-branch
  bugfix-branch

In this example, the current branch is main.

  1. Use the git branch command with the -m option and the new name you want to give to the branch. For example, if you want to rename the current branch to new-main, you can use the following command:
$ git branch -m new-main

This will rename the current branch from main to new-main.

  1. Verify that the branch has been renamed by using the git branch command again:
$ git branch
* new-main
  feature-branch
  bugfix-branch

Now, the current branch is new-main.

Note: Renaming a branch only affects the local repository. If you want to rename a branch in a remote repository, you will need to push the changes to the remote repository using the git push command.

Example:

Suppose you have a branch named feature-branch and you want to rename it to new-feature-branch. To do this, you can use the following command:

$ git branch -m new-feature-branch

This will rename the current branch feature-branch to new-feature-branch. You can then use the git branch command to verify that the branch has been renamed:

$ git branch
* new-feature-branch
  main
  bugfix-branch

Now, the current branch is new-feature-branch.