Made a small mistake while naming your git branch, don't panic this blog post will go through all the possibilities for renaming the git branch on local and remote.
Git branch can be renamed with a simple command "git branch -m <new-name>", this will rename your current git branch name. Once the branch is renamed on local you can push the new branch to remote and delete the old git branch on the remote.
Now let's go through some detail on why and how to rename a git branch on local and remote.
Why you might want to rename the git branch name?
Before going into details of how to rename local and remote git branches, let's see the reason why you might want to do it:
- You might want to rename your git branch if the branch name does not match your project branch naming standards.
- You might have the wrong branch name or a wrong ticket number in your branch name.
- Sometimes you might make a typo mistake while naming your branch and want to make it perfect by renaming it.
How to rename the local git branch?
Now let's see how to rename a git branch on local with these steps:
- The first step to rename a branch is to checkout the branch you might want to rename.
$ git checkout <old-name>
- Now you can run the "git branch" command with -m and the new branch name
$ git branch -m <new-name>
- To check if the branch is renamed correctly you can run the "git branch" or "git status" command to check your current branch
$ git branch
$ git status On branch new-name nothing to commit, working tree clean
If your old git branch was only on your local and not pushed to remote then you can stop here.
But if you have the wrong branch name on remote as well then continue reading.
How to rename remote git branch?
In the previous stage, we rename the git branch on local and now we will follow these steps to update the branch name on remote as well.
- Once the branch is renamed on local, run the below command to push the renamed branch to remote
$ git push origin -u <new-name>
- Pushing the branch to remote with the new name will now delete the old branch on the remote. You need to run the below command to delete the remote branch with the old name.
$ git push origin --delete <old-name>
Okay, now we know that there is no need to panic if you made a mistake while naming your git branch.
We have seen why might need to rename your git branch, how to rename a branch on local, and then how to push it to the remote repository if required.
And always remember to remove the old git branch from the remote to make sure no one uses it.
Leave a comment if you need to know more about renaming your git branch and do check out our blog post on basic git commands you should know.
Add new comment