KKAIROS
Practical guideIllustrative4 min read

Git Commit Fixup: correct an earlier commit

git commit --fixup COMMIT records a commit marked for COMMIT. A later git rebase -i --autosquash moves it next to COMMIT and folds it in.

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-101-A: fixup plus autosquash folds the correction into the earlier commit and leaves two commits above base

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 'base\n' > b.txt; git add b.txt; git commit -qm base
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'x\n' > g.txt; git add g.txt; git commit -qm two
printf 'a2\n' > f.txt; git add f.txt

Command:

git commit -q --fixup HEAD~1; echo before=$(git log --oneline | wc -l); GIT_SEQUENCE_EDITOR=true GIT_EDITOR=true git rebase -q -i --autosquash HEAD~3; echo after=$(git log --oneline | wc -l); git log --format=%s; git show HEAD~1:f.txt

Recorded output:

before=4
after=3
two
one
base
a2

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

TE-101-B: the fixup commit subject is fixup! followed by the subject of the target commit

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 'base\n' > b.txt; git add b.txt; git commit -qm base
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'a2\n' > f.txt; git add f.txt

Command:

git commit -q --fixup HEAD; git log -1 --format=%s

Recorded output:

fixup! one

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

TE-101-C: expected non-zero exit: --fixup with a name that is not a commit stops with a fatal error and records nothing

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 'base\n' > b.txt; git add b.txt; git commit -qm base
printf 'a\n' > f.txt; git add f.txt

Command:

git commit -q --fixup nosuchref; rc=$?; echo commits=$(git log --oneline | wc -l); exit $rc

Recorded output:

commits=1

Standard error from the same run:

fatal: could not lookup commit nosuchref

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

Reading the recorded output

The setup left three commits, base, one and two, and a changed f.txt in the index. The line before=4 is the commit count after git commit --fixup HEAD~1, which added a fourth commit. After the rebase with --autosquash, after=3 shows that the fixup commit was folded into another one. The subject list, two, one, base, has the newest commit first, and no subject starts with fixup. The last line, a2, is the content of f.txt in HEAD~1, which is the commit one, so the correction is inside it.

TE-101-B: git log -1 --format=%s printed fixup! one, which is fixup! followed by the subject of the commit that was named; by the setup, HEAD was the commit one. TE-101-C: for a name that is not a commit, git printed fatal: could not lookup commit nosuchref and exited with status 128. The line commits=1 shows that no commit was recorded.

Ad

Limits

The run sets GIT_SEQUENCE_EDITOR=true so the rebase list is accepted without an editor. Rebasing commits that others already have rewrites shared history, and the run has no remote. Conflicts were not tested.

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-101-A | fixup plus autosquash folds the correction into the earlier commit and leaves two commits above base | 0 | PASS | 912af2643c2f | | TE-101-B | the fixup commit subject is fixup! followed by the subject of the target commit | 0 | PASS | 28c64b60c8fd | | TE-101-C | expected non-zero exit: --fixup with a name that is not a commit stops with a fatal error and records nothing | 128 | PASS | 9190acfba864 |

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