You may be working on a feature named good-feature
. As you work on it, an inspiration strikes and
you decide to rename your feature as unicorn-feature
. To keep things consistent, you want to rename your feature
branch from good-feature
to unicorn-feature
. You might already be deep into development and
pushed quite a few changes to remote.
In this post, I will tell you:
- How to rename a branch in your local Git repository?
- How to rename the corresponding branch in remote?
Renaming local Git branch
The command is:
git branch --move <old-name> <new-name>
In our example, the command would be:
git branch --move good-feature unicorn-feature
How do you rename the branch in remote too?
You simply treat the branch - with the new name - as a new branch, and push the branch to remote.
git push --set-upstream origin <new-name>
In our case, the command would be:
git push --set-upstream origin unicorn-feature
This will create a new branch in our remote (origin) named unicorn-feature
. However, the old
branch still remains in remote. To verify, you can run the git branch --all
to list all the branches - including
the ones in remote. The next step, then, is to delete the old branch (good-feature
) from remote.
Deleting the old branch from remote
In an earlier post about Git tags, I had written about the following command:
git push <remoteName> --delete <gitReference>
In our case, we need to run:
git push origin --delete good-feature
Final words
Since now, you won’t be stuck with a branch name once chosen. You can always rename it later.