How to delete a git branch locally and remotely in 1 minute?

Written by Shan Shah
Published on July 6, 2021
2 mins read
how to delete a git branch locally or remotely

Do you want to delete a git branch locally or remotely and looking for a secure and better solution to fulfill your need?

This guide will take you through step by step to delete a git branch locally or remotely.

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.

Note:
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.

Case Statement

A "develop" branch is the main branch. Once the main features complete, all developers need to merge their branch into develop branch.

You are working on a feature-register branch.

You have completed the feature and now want to merge it into develop branch. Command you will run:

git checkout develop

Now, you have moved into develop branch and want to delete the feature-register branch.

Delete a git branch locally

The method is pretty straightforward,

git branch --delete feature-register

OR

git branch -d feature-register (Shorter version)

-d is an alias of --delete

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.

Delete a git branch Remotely

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

OR

git push -d origin 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 TWEET

How to delete a git branch locally and remotely? Click To Tweet
Become a better Web Developer in 15 minutes a week. Join Now 📩
We hate spam, unsubscribe anytime.
Let's Discuss 🚀
JOIN FACEBOOK GROUP
Coder Champ © 2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram