Skip to content

How to Fix Git Cannot Delete Branch Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Git Cannot Delete Branch Error. We cover key concepts, practical examples, and best practices.

You run git branch -d feature and get error: The branch 'feature' is not fully merged — Git prevents deletion of branches that have commits not present in the current branch.

The Problem

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

Step-by-Step Fix

Step 1: Force delete a local branch

# WRONG — refuses for unmerged branches
git branch -d feature

# RIGHT — force delete even if unmerged
git branch -D feature

Step 2: Delete a remote branch

git push origin --delete feature

Or:

git push origin :feature

Step 3: Switch off the branch first

You cannot delete the currently checked-out branch:

git checkout main
git branch -D feature

Step 4: Delete a branch that has been merged

If the branch is fully merged:

git branch -d feature

Step 5: Delete multiple branches

# Delete branches matching a pattern
git branch -d $(git branch --list 'fix/*')

# Delete all merged branches except main
git branch --merged | grep -v '\*\|main\|develop' | xargs git branch -d

Step 6: Prune remote-tracking branches

git remote prune origin

This removes local references to remote branches that no longer exist.

Prevention Tips

  • Delete branches after merging to keep the repository clean
  • Use branch naming conventions (feature/, fix/, chore/)
  • Automate deletion of merged branches with Git hooks
  • Set up branch protection rules on remote
  • Use git branch -d for safe deletion, -D only when sure

Common Mistakes with branch delete

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world GIT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between git branch -d and -D?

git branch -d safely deletes only fully merged branches. git branch -D force deletes regardless of merge status. Use -d for routine cleanup and -D when you are sure the branch is not needed.

How do I delete a branch on a remote repository?

Use git push origin --delete <branch> or git push origin :<branch>. On GitHub/GitLab, you can also delete branches from the web interface after a pull request is merged.

Can I recover a deleted branch?

Yes, if you know the commit hash. Use git branch <branch-name> <commit-hash> to recreate it. Find the hash with git reflog if you deleted it recently. Remote branches can be re-fetched.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro