Skip to content
LatchkeyLatchkey home

git branch -d / -D: Usage, Options & Common CI Errors

git branch -d safely deletes a merged branch; -D forces deletion regardless of merge state.

Branch cleanup is routine after merges. The -d/-D distinction is a safety gate: -d refuses to drop unmerged work, while -D overrides that check.

What it does

git branch -d deletes a local branch only if it is fully merged into its upstream or HEAD. git branch -D (a synonym for --delete --force) deletes it unconditionally, discarding any commits unique to it.

Common usage

Terminal
git branch -d feature           # only if merged
git branch -D feature           # force, even if unmerged
git branch -d -r origin/old     # delete a remote-tracking ref locally
git branch --merged main | grep -v '\*' | xargs -r git branch -d

Options

FlagWhat it does
-d / --deleteDelete a merged branch
-DForce-delete (delete + force)
-rOperate on remote-tracking refs
--merged / --no-mergedFilter branches by merge state

Common errors in CI

error: The branch 'X' is not fully merged. If you are sure you want to delete it, run 'git branch -D X' - -d protects unmerged commits; use -D to override. error: Cannot delete branch 'X' checked out at '<path>' means it is the current branch or used by a worktree; switch away first.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git branch -d / -D: Usage, Options & Common CI Errors?
Branch cleanup is routine after merges. The -d/-D distinction is a safety gate: -d refuses to drop unmerged work, while -D overrides that check.
What it does?
git branch -d deletes a local branch only if it is fully merged into its upstream or HEAD. git branch -D (a synonym for --delete --force) deletes it unconditionally, discarding any commits unique to it.
Common errors in CI?
error: The branch 'X' is not fully merged. If you are sure you want to delete it, run 'git branch -D X' - -d protects unmerged commits; use -D to override. error: Cannot delete branch 'X' checked out at '<path>' means it is the current branch or used by a worktree; switch away first.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card