The above screenshot is the Git client — Fork which I used daily basis for commit code, writing my commit message, and etc.
- Right-click a branch
- Choose “Rename”.
- Type in your new desired branch name
- Just tick the checkbox and it helped you change your local branch name and remote branch name. ( Like the screenshot above )
However, I am wondering how the branch rename works under the hood. Is it as easier as what the UI flow showed us or what we saw was just the abstract of the complex work?
If you were interested in what is the action under the hood for the branch rename, then this post might be helpful to you. Without further ado, let’s start.
Rename Branch Under the Hood
For this section, we would separate it into two parts.
- Rename a local branch (Easiest one)
- Rename a remote branch
Rename Local Branch
Rename a local branch was easier and it can simply just done using a single command.
git branch -m old_branch new_branch
For e.g, you would like a local branch named feature/payment to feature/deposit. Here is the command you need to run.
git branch -m feature/payment feature/deposit
Rename Remote Branch
If you already push your branch to the remote, which means your code repository in the cloud. Then you need more steps to rename the local & remote branch
- Perform a local rename
- Push the latest renamed local branch to remote
- Delete the old remote branch
Now, you shall realize rename a remote branch is similar to create a new branch with a new name, after that delete the old branch. Let’s see how we do it via the CLI.
# 1.) Rename a branch
git branch -m old_branch new_branch# 2.) Push latest renamed local branch to remote and reset upstream
git push origin -u new_branch# 3.) Delete the old remote branch
git push origin -d old_branch
Something worth mention here is the 2nd command
-u options. It basically allows you to reset your new branch upstream tracking. Otherwise, it would still be tracking the old_branch as remote upstream.
Lastly, you might think do I need to really know this complicated command stuff? Why not just use the Git client.
- Right-click, type something, tick the checkbox and click the button. ( Boom, all done ✅ )
Well, it really depends on your preference. It’s just like everyone has their own preference.
- Some like hot & spicy food
- Some like to eat chicken breast ONLY.
So feel free to have your own preferred ways of doing stuff. However, the CLI command allows you to use the git command in server environment where you do not have the User Interface.
I hope you enjoy this article and I will see you in the next article.