Git is an extremely popular open-source version control system that might look complicated, but once you get the hang of it, it will make your life very easy. In this guide, we will cover the basics of how to rename your local and remote git branches. Read on!
How To Rename a Local Git Branch
Renaming a local git branch is easy and you can do it in as little as one command!
#1. Navigate to the branch you want to rename. You can do this by using the following command:
$ git checkout <branch_name>
#2. Now, you can rename your original branch:
$ git branch -m <new_name>
If you are on a different branch, you can instead use a different command.
$ git branch -m <old_name> <new_name>
That's all! At this point, you should have successfully renamed your local git branch.
You can verify the names of your local branches with the command -
git branch, which will show you a list of all your remote branches.
How To Rename a Remote Git Branch
Once you've followed the steps outlined above and deleted your local git branch, you might want to rename its remote copy as well (if you have already pushed the original branch). There is no direct command to this. Instead, you will have to delete the original remote branch and then push the renamed local one. Here's how.
#1. First, you will have to delete the remote git branch. To do this, type one of the following into the terminal:
$ git push <remote> --delete <branch_name> or $ git push <remote> :<branch_name>
<remote> is the name of your remote repository. In most cases, it tends to be
#2. Now, you can push the renamed branch to
<origin> and ask your new local branch to track your new remote branch.
git push origin -u <new_name>
And you're done!