git — Rename and Delete Branches
Silly mistakes are common while typing and I do them a lot. But think if it happens while naming any branch in git; you have to react quickly before anyone finds it, right?. And thankfully it is possible to correct them in git and it is very easy.
If you are a new to git, you might find these words very often in the article: local and remote.
local — is the code on your machine/device and remote —is the code repository hosting services like GitHub, GitLab, Atlassian BitBucket or even your own git server where you maintain all of your code…
So this post covers
✅ Renaming a branch locally and Updating it in the remote
✅ Deleting a branch in the local and in the remote
Step-1: Renaming your local branch
- type
git branch
to get the list of all branches.
if you are in the same branch
git branch -m new-name
if you are not in the same branch
git branch -m old-name new-name
Step-2: Pushing changes to the remote
if the renamed branch is not pushed to the remote then you can push it normally by
git push origin new-name
if the renamed branch is already there on the remote (if you push it using the above method, it will create a new branch instead of replacing it) use
git push origin :old-name new-name
Step-1: Deleting only in the local
if you want to delete a branch only in the local and not in the remote you can use this
git branch -d branch_name -or-git branch -D branch_name
use -D only If there are un-merged changes which you want to delete.
Step-2: Deleting only in the remote
You want a branch to present in the local but not in the remote, just do this
git push origin --delete branch_name
Step-3: Deleting in both local and remote
if this is the case, then
Complete Step-1 and do Step-2 or vice-versa.
And that’s it, you got your back ;P
Adios Amigos.
Thanks for reading my post. I’d like to hear your thoughts in comments. If you like the article, please click the💚 below so that more people may read it.