Git Commit Allow Empty: a commit with no changes
git commit with nothing staged refuses; in this run the exit status was 1. git commit --allow-empty -m MESSAGE records a commit with no file changes.
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-92-A: a plain commit with nothing staged fails, --allow-empty succeeds
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 oneCommand:
git commit -qm nothing >/dev/null 2>&1; echo plain=$?; git commit -q --allow-empty -m marker; echo empty=$?; echo commits=$(git log --oneline | wc -l); git show --stat --format=%s HEAD | head -1Recorded output:
plain=1
empty=0
commits=2
markerExit status: 0. The output matches the expected text written before the run. This is the status of head, the last command in the pipe.
TE-92-B: an empty commit changes no file between its parent and itself
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 oneCommand:
git commit -q --allow-empty -m marker; git diff --name-only HEAD~1 HEAD | wc -l; git log -1 --format=%sRecorded output:
0
markerExit status: 0. This status and the output match what was written before the run. This is the status of git log, the last command.
TE-92-C: expected non-zero exit: git commit with nothing staged prints that there is nothing to commit and exits with status 1
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 oneCommand:
git commit -m nothingRecorded output:
On branch main
nothing to commit, working tree cleanExit status: 1. This status and the output match what was written before the run.
TE-92-B: the empty commit was made, git diff --name-only between its parent and itself printed no file name, so the count is 0, and the newest subject is marker. TE-92-C: git commit -m nothing in a clean repository exited with status 1. Standard output has the two lines On branch main and nothing to commit, working tree clean.
Limits
TE-92-A shows the exit status and the commit count, TE-92-B shows that the empty commit changes no file, and TE-92-C shows the refusal text. Using empty commits to trigger other tools was not tested.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-92-A | a plain commit with nothing staged fails, --allow-empty succeeds | 0 | PASS | 2d11513d490e | | TE-92-B | an empty commit changes no file between its parent and itself | 0 | PASS | 07f74dee457f | | TE-92-C | expected non-zero exit: git commit with nothing staged prints that there is nothing to commit and exits with status 1 | 1 | PASS | 84184ee30d90 |
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