KKAIROS
Practical guideIllustrative4 min read

Git Branch Delete: -d versus -D

git branch -d NAME refuses to delete a branch that is not merged into HEAD (or its upstream). git branch -D NAME deletes it anyway.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-61-A: -d refuses an unmerged branch, -D deletes it

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
git checkout -q -b side; printf 's\n' > s.txt; git add s.txt; git commit -qm side
git checkout -q main

Command:

git branch -d side; echo d_exit=$?; git branch -D side | sed 's/was [0-9a-f]*/was HASH/'; echo branches=$(git branch --list | wc -l)

Recorded output:

d_exit=1
Deleted branch side (was HASH).
branches=1

Standard error from the same run:

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

Exit status: 0. The output matches the expected text written before the run.

TE-61-B: -d deletes a branch that is already merged into HEAD, with exit status 0

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
git checkout -q -b side; printf 's\n' > s.txt; git add s.txt; git commit -qm side
git checkout -q main; git merge -q --ff-only side

Command:

git branch -d side | sed 's/was [0-9a-f]*/was HASH/'; echo branches=$(git branch --list | wc -l)

Recorded output:

Deleted branch side (was HASH).
branches=1

Exit status: 0. The output matches the expected text written before the run.

TE-61-C: expected non-zero exit: deleting a branch name that does not exist is a different error from not fully merged

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base

Command:

git branch -d nosuch

Recorded output: none, standard output was empty.

Standard error from the same run:

error: branch 'nosuch' not found.

Exit status: 1. This status and the output match what was written before the run.

Reading the recorded output

The first command, git branch -d side, printed nothing to standard output and wrote the error The branch 'side' is not fully merged to standard error. The line d_exit=1 is its exit status. The second command, git branch -D side, printed Deleted branch side (was HASH). The sed command replaced the real commit id with the word HASH, so the id is not part of the record. The last line, branches=1, is the number of lines printed by git branch --list after the delete; by the setup, the one branch left is main.

TE-61-B: by the setup, side was merged into main with a fast-forward, so -d deleted it with exit status 0 and printed Deleted branch side (was HASH). One branch is left. TE-61-C: the name nosuch does not exist, and git branch -d printed error: branch 'nosuch' not found. with exit status 1. This is a different message from the not fully merged error in TE-61-A.

Ad

Limits

The git-branch docs (DESCRIPTION section, the paragraph on -d and -D) say: "If the branch currently has a reflog then the reflog will also be deleted." The commit may still be found through the HEAD reflog or git fsck until garbage collection. This run does not test recovery. The run covers a branch without an upstream only.

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-61-A | -d refuses an unmerged branch, -D deletes it | 0 | PASS | d812cad6bacd | | TE-61-B | -d deletes a branch that is already merged into HEAD, with exit status 0 | 0 | PASS | 0ad4d5f065e9 | | TE-61-C | expected non-zero exit: deleting a branch name that does not exist is a different error from not fully merged | 1 | PASS | 8a68cf8d1d44 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides