Skip to content

How to Force Delete a Git Branch That Is Not Fully Merged

DodaTech 2 min read

In this tutorial, you'll learn about How to Force Delete a Git Branch That Is Not Fully Merged. We cover key concepts, practical examples, and best practices.

The Problem

Running git branch -d feature-branch fails with error: The branch 'feature-branch' is not fully merged because Git refuses to delete a branch whose commits are not reachable from the current branch or its upstream.

Quick Fix

Force Delete with -D (Capital D)

git branch -D feature-branch
# Deleted branch feature-branch (was abc1234)

git branch -D is equivalent to git branch --delete --force. It deletes the branch even if its commits are not fully merged into the current branch or any other ref.

Verify You Do Not Need the Branch First

git log feature-branch --oneline --not --all
# abc1234 WIP: experimental feature
# def5678 refactor: unfinished changes

Use git log <branch> --not --all to list commits in the branch that are not reachable from any other ref. Review these commits before force-deleting to ensure nothing important is lost.

Squash the Branch and Merge Before Deleting

git merge --squash feature-branch && git commit -m "Add feature"
git branch -d feature-branch
# Deleted branch feature-branch (was abc1234)

If you want to keep the changes but in a single commit, squash-merge first before deleting. The regular -d deletion will succeed once the branch content has been incorporated.

Recover a Force-Deleted Branch

git reflog
# abc1234 HEAD@{2}: checkout: moving from feature-branch to main
git checkout -b feature-branch-recovered abc1234
# Switched to a new branch 'feature-branch-recovered'

A force-deleted branch can be recovered from the reflog as long as you act within the reflog expiry period (90 days by default). The commit hash remains in the reflog.

Practice with a Test Repository

cd /tmp
mkdir git-practice && cd git-practice
git init --initial-branch=main
# Initialized empty Git repository in /tmp/git-practice/.git/
echo "test" > file.txt && git add . && git commit -m "init"
# [main (root-commit) abc1234] init

Before running destructive commands on your real repository, practice on a throwaway test repository. This builds confidence and prevents costly mistakes. The reflog is your safety net, but practice makes it less needed.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention

  • Use -d (lowercase) by default and switch to -D only after confirming the branch is truly disposable
  • Review unmerged commits with git log <branch> --not --all before force-deleting
  • Push long-running feature branches to the remote as a backup
  • Archive branch names you delete in a text file if you might need the commits later

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro