How to Fix Git Cannot Delete Branch Error
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 -dfor safe deletion,-Donly when sure
Common Mistakes with branch delete
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro