Skip to main content

Using Git

How to Delete Local Git Branch

In this chapter of the Git beginners tutorial series, learn about deleting and force deleting a local git branch.

Git is an amazing version control tool. It allows you to have multiple branches to isolate your code from the main branch. Which, in turn helps you work with multiple people at once.

In the previous article, you learned about creating branches in Git. Learn about deleting them in this one.

Delete a branch with -d option of git branch command:

git branch -d

Force delete a branch (if there are comments to the branch) with option -D:

git branch -D

Let's see it in detail.

Get branch name

Your branch will always be the "master" branch (unless specified otherwise).

To check which branch you are on and all the available local branches, use this command:

git branch

It shows the following two branches for me. The one with asterisk (*) is the current branch:

$ git branch
* master
  test-lhb

This way, you get the exact branch names.

Delete a single branch

The easiest way to delete a Git branch is using the -d flag along with the git branch command and also specifying the branch name. Below is the syntax:

git branch -d <BRANCH-NAME>

So, when I want to delete the test-lhb branch, I would run the following command:

$ git branch -d team-lhb
Deleted branch test-lhb (was 1719dd9).

But, when the branch you are deleting has either one of the following conditions met, Git will not allow you to delete that branch with the -d flag.

  • Either the commits in that branch are not merged with 'master' branch.
  • If the commits in that branch are not pushed to a remote repository.

Below is an example of how it might look when deleting such a branch:

$ git branch -d test-lhb
error: The branch 'test-lhb' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test-lhb'.

When you are faced with this situation, and if you are sure about deleting said branch, use the -D flag instead of -d flag using the same syntax:

git branch -D <BRANCH-NAME>

So, when I want to forcefully delete a branch, I would run the following command:

$ git branch -D test-lhb
Deleted branch test-lhb (was d2ec7fe).

As you can see, the test-lhb branch is now deleted forcefully.

Bonus Tip: Undo branch deletion

Well, let's say you deleted the wrong branch by mistake. In case you want to undo the action, please make sure that you have the SHA1 sum of the branch you deleted.

When you delete a branch, Git shows the SHA1 sum of that branch.

$ git branch -D test-lhb
Deleted branch test-lhb (was d2ec7fe).

Using the same command (and output) as above, as you can see, SHA1 sum is provided at the end - which is d2ec7fe.

Now, use the following syntax to restore the deleted branch.

git branch <BRANCH-NAME> <SHA1-SUM>

So, in my case, if I want to restore the test-lhb branch, I would run the following command.

$ git branch test-lhb d2ec7fe

$ git branch
  master
* test-lhb

Now, you can see that the test-lhb branch is restored with its contents intact. You can feel relieved now.

Deleting a remote branch

Deleting a remote git branch is not the same as deleting the local ones.

git push origin --delete <remote-branch-name>

If you want more details, we have a separate tutorial on the topic.

How to Delete Remote Git Branch
In this article of Git beginner tutorial series, learn about deleting a remote Git branch.

With this Git beginner tutorial, you now know how to delete, force delete, and restore a deleted Git branch. Enjoy Git!

Team LHB