Whenever you work on a new feature in Git, it may go through different name changes even as it gets deeper into development. It’s important to know how to rename a Git branch to keep things consistent.
Git Branch Rename Explained
Renaming a Git branch is an important step to ensure consistent naming of your features as you make changes in development. You can rename a Git branch with the following command line: git branch --move
.
For example, 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
. You’ll want to rename your feature branch from good-feature
to unicorn-feature
for consistency. However, 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.
How to Rename a 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 to Rename a Git Branch in Remote
To rename the Git branch in remote, 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.
If you need to delete an old branch, you’d enter:
git push <remoteName> --delete <gitReference>
And that’s everything you need to know about renaming a git branch.
Frequently Asked Questions
How do you rename a git branch?
You can rename a local git branch using the command: git branch --move <old-name> <new-name>.
How do you rename a Git branch in remote?
You can rename a Git branch in remote with the command: git push --set-upstream origin <new-name>.