Managing branches is tricky when working with git, especially when you want to delete a branch locally or remotely.
It's often hard to remember how to delete a git branch.
It doesn't have to be that hard. This guide will walk you through deleting branches locally and remotely in just 1 minute.
Git branch deletion is a common task for Web developers.
But sometimes, you might have to delete a branch that you have already used, or you might have made mistakes in your git command and want to delete a git branch locally or remotely.
Suppose you want to delete any git branch, whether locally or remotely. You will always need to checkout from the branch you want to delete.
Let's say a repository has a master branch.
Once the main features are complete, all developers need to merge their branch into the master branch.
You are working on a feature-register branch.
You have completed the feature and now want to merge it.
Command you will run:
git checkout master
Now, you have moved into the master branch and want to delete the feature-register branch.
The method is pretty straightforward,
git branch --delete feature-register
git branch -d feature-register (Shorter version)
-d is an alias of --delete
What about when you push the branch to a remote hub like GitHub or Bitbucket?
And now you want to delete that specific branch.
At this point, you can utilize the commands mentioned below:
git push --delete origin feature-XYZ
git push -d origin feature-XYZ
No. To delete a git branch locally, you must first merge the feature branch into another one and then destroy the original local branch.
Sometimes, you complete a feature that never merges because there is no longer need for that created feature.
For this case, none of the above commands will work. It will throw an error like:
"error: The branch 'feature-XYZ is not fully merged."
If you are sure you want to delete it, run 'git branch -D feature-XYZ.'
If you want to delete an unmerged branch forcefully, you will have to use:
git branch -D feature-XYZ.
Looking for this article? : 10 Ways to earn money online as a Web Developer
We have a CSS Mastery series going on our Youtube channel: Coder Champ
You can now tweet this guide with your followers by using CLICK TO TWEETHow to delete a git branch locally and remotely? Click To Tweet