Git Commit Amend: replace the last commit
git commit --amend replaces the last commit with a new one. The new commit has a different id. Add -m to change the message.
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-63-A: amend changes the message and the commit id, and the commit count stays the same
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 'old message'
git rev-parse HEAD > .beforeCommand:
git commit -q --amend -m 'new message'; echo count=$(git log --oneline | wc -l); echo subject=$(git log -1 --format=%s); echo same_id=$([ "$(git rev-parse HEAD)" = "$(cat .before)" ] && echo yes || echo no)Recorded output:
count=1
subject=new message
same_id=noExit status: 0. The output matches the expected text written before the run.
TE-63-B: amend with --no-edit keeps the message and folds a newly staged file into the last 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 'a\n' > f.txt; git add f.txt; git commit -qm 'old message'
printf 'g\n' > g.txtCommand:
git add g.txt; git commit -q --amend --no-edit; echo count=$(git log --oneline | wc -l); echo subject=$(git log -1 --format=%s); git show --name-only --format= HEAD | sortRecorded output:
count=1
subject=old message
f.txt
g.txtExit status: 0. This status and the output match what was written before the run. The status is that of sort, the last command in the pipe.
TE-63-C: expected non-zero exit: amend in a repository with no commit has nothing to amend
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 falseCommand:
git commit --amend -m xRecorded output: none, standard output was empty.
Standard error from the same run:
fatal: You have nothing to amend.Exit status: 128. This status and the output match what was written before the run.
TE-63-B: git add g.txt followed by git commit --amend --no-edit left one commit with the subject old message. The last commit now lists f.txt and g.txt. TE-63-C: in a repository with no commit, git commit --amend -m x exited with status 128 and standard error reads fatal: You have nothing to amend.
Limits
Amending a commit that others already pulled rewrites shared history. This run uses one repository and no remote.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-63-A | amend changes the message and the commit id, and the commit count stays the same | 0 | PASS | dc24330d7a5c | | TE-63-B | amend with --no-edit keeps the message and folds a newly staged file into the last commit | 0 | PASS | 2b77531f4cdb | | TE-63-C | expected non-zero exit: amend in a repository with no commit has nothing to amend | 128 | PASS | 4d0b17fb13c1 |
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