You can rename a local branch in git using the git branch -m command. On Windows, you can use -M instead of -m so that git knows and respects the case in the name.
Let us see a real-time example.
The initial setup is as follows.
git branch shows that there are two branches main and maincopy. The asterisk denotes that we are currently in maincopy branch.
git status shows it is up to date with origin. git remote -v shows the list of remote repositories. origin is linked to repoone git repository.
~/repoone$ git branch main * maincopy ~/repoone$ git status On branch maincopy Your branch is up to date with 'origin/maincopy'. nothing to commit, working tree clean ~/repoone$ git remote -v origin firstname.lastname@example.org:codeversionmaster/repoone.git (fetch) origin email@example.com:codeversionmaster/repoone.git (push) repotwo firstname.lastname@example.org:codeversionmaster/repotwo.git (fetch) repotwo email@example.com:codeversionmaster/repotwo.git (push)
To rename current branch
To rename the branch we are checked in, use git branch -m. Here, we are trying to rename maincopy to newname.
~/repoone$ git branch -m newname
Using git branch, we can see that the branch maincopy gets renamed to newname.
~/repoone$ git branch main * newname
But wait, what is it linked to?
git status shows that it is still linked to origin/maincopy branch, i.e. its name is still maincopy in the remote repository.
~/repoone$ git status On branch newname Your branch is up to date with 'origin/maincopy'. nothing to commit, working tree clean
What to do next depends on if we intend to rename the branch even remotely or push it as a new branch into the remote repository.
If we intend to push it as a new branch, we should first change its upstream link and then push it. We can do this in one step, as shown.
~/repoone$ git push --set-upstream origin newname Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'newname' on GitHub by visiting: remote: https://github.com/codeversionmaster/repoone/pull/new/newname remote: To github.com:codeversionmaster/repoone.git * [new branch] newname -> newname Branch 'newname' set up to track remote branch 'newname' from 'origin'.
git status would now show that the upstream link is origin/newname and is now present in the remote repository.
~/repoone$ git status On branch newname Your branch is up to date with 'origin/newname'. nothing to commit, working tree clean
Will it rename if a branch exists already with the new name?
Consider this situation where we have two branches main and existingname. We want to rename main to the same name as another existing branch. Here it is existingname.
~/repoone$ git branch existingname * main
In this case, git branch -m would throw an error, as shown.
~/repoone$ git branch -m existingname fatal: A branch named 'existingname' already exists.
Rename a branch that is not checked in
Here, we are checked in existingname branch, and we want to rename main branch as newname.
~/repoone$ git branch * existingname main
Then, we use git branch -m with two arguments, the first argument being the current name and the second argument will be the new name.
As you can see, git branch now shows the name of main branch changed to newname.
~/repoone$ git branch -m main newname ~/repoone$ git branch existingname * newname
Note: This command can be used even if we are currently in the branch we intend to rename.