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

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

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.

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

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.

How to delete a git branch Locally?

how to 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

How to delete a git branch Remotely?

how to 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

FAQs:

how to delete a git branch locally and remotely

Is it safe to delete the local branch in git?

No. To delete a git branch locally, you must first merge the feature branch into another one and then destroy the original local branch.

Is there any way to delete a local branch without merging it into master first?

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 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.
Coder Champ © 2021 - 
Privacy Policy
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram