How To Change Branch Name on Git

Git Branches are mainly used for the development of the main project smoothly and in a decided workflow. At some times, developers may think to change the branch names in git for pushing the changes in that specific branch. Choosing the branch name will always depend upon what you are working on.

Renaming the branch name in Git is very simple using the related git commands. If you want to change your git branch names then take a look at the entire tutorial on How do you Change Branch Name on Git easily.

This guide will help you rename git branches locally and remotely. So, check out the available stuff regarding Git Rename Branch from here.

Change Branch Git Name

In order to change a branch name on Git, you have to use the “git branch” command followed by the “-m” option. Next, you just have to specify the name of the new branch.

# Optional command (if you are not on the target branch)
$ git checkout <branch>

$ git branch -m <new_branch_name>

Note: Before changing the branch name, make sure to switch to the branch that you want to rename.
It is important to mention that this command changes the name of your local branch.

If you want your changes to be available on the remote side, you have to push your branch to the remote.

To achieve that, you have to use the “git push” command and specify the old branch name as well as the new branch name.

$ git push <remote> :<old_branch_name> <new_branch_name>

Finally, you have to set the upstream branch for the newly created branch using the “git upstream” command.

$ git push <remote> -u <new_branch_name>

In order to illustrate this method, let’s observe a quick example from the below modules.

Also Read: How To Change Git Remote Origin

Git Rename Branch

The git branch command allows you to change a branch. For renaming a branch, the command that should run is git branch -m <old> <new>. “old” is the name of the branch you want to rename and “new” is the new name for the branch.

Look at the following syntax for the Git rename branch command:

git branch -m <old> <new>

Examples on Git Rename Branch

In the below examples, we are going to rename one of our branches currently named “feature”.

First of all, we are going to check on which branch we are at the moment with the “git branch” command.

$ git branch

Example changing a branch name git-branch-command

In this case, we are already on the “feature” branch, so we do not need to switch to any other branches.

Git Rename Local Branch Example

Now that we are on our “feature” branch, we are going to change the branch name to “quickfix” using the branch command.

$ git branch -m "quickfix"

Example changing a branch name git-change-branch-name

As you can see, the “local” branch name was changed to “quickfix“, but this does not mean that your branch name was updated on the remote!

Actually, if you run the “git branch” with the “-a” option (for “all”), you can see that the upstream branch is still pointing to the “feature” branch.
Example changing a branch name git-display-all-branches

Git Rename Remote Branch Example

To change the name of the remote, you are going to push the updated references to your remote.

$ git push origin :"feature" "quickfix"

Example changing a branch name git-update-remote-1

As you can see, two operations were actually performed here :

  • the “feature” branch was deleted from the remote
  • the “quickfix” branch was created on the remote.

However, updating your references does not mean that the upstream branch was actually updated.

To update your new upstream branch, you need to use the “push” command.

$ git push origin -u quickfix

How To Change Branch Name on Git set-upstream-branch-git

Done!

Congratulations, you successfully changed the name of your branch on Git!

Conclusion

In this tutorial, you learned how you can change the name of your branch on Git easily.

You learned that you have to change the name of your branch locally and on the remote, otherwise you might run into some inconsistencies on your repository.

If you are interested in software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!

Leave a Reply

Your email address will not be published. Required fields are marked *