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.
- How To Create a Git Branch | Learn Git Create New Branch from Current Branch
- How To Switch Branch on Git | What is Git Switch? | Git Switch vs Checkout
- How To Clean Up Git Branches | Git Clean Up Local and Remote Branches
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
- Git Rename Branch
- Examples on Git Rename Branch
- Git Rename Local Branch Example
- Git Rename Remote Branch Example
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
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>
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
In this case, we are already on the “feature” branch, so we do not need to switch to any other branches.
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"
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!
To change the name of the remote, you are going to push the updated references to your remote.
$ git push origin :"feature" "quickfix"
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
Congratulations, you successfully changed the name of your branch on Git!
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!