# Git Commit Amend: replace the last commit

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-commit-amend-full-en
`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.

(Test: 3 tests were run, TE-63-A, TE-63-B and TE-63-C. The expected non-zero exit is TE-63-C (exit status 128). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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 > .before
```

Command:

```bash
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=no
```

Exit 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`):

```bash
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.txt
```

Command:

```bash
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 | sort
```

Recorded output:

```
count=1
subject=old message
f.txt
g.txt
```

Exit 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`):

```bash
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
```

Command:

```bash
git commit --amend -m x
```

Recorded 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

- https://git-scm.com/docs/git-commit

## 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.

## Related guides
- [Git Show: read one commit](https://kairos-2z6s.onrender.com/res/git-show-full-en)
- [Git Reflog: the history of where HEAD pointed](https://kairos-2z6s.onrender.com/res/git-reflog-full-en)
- [Git Commit Fixup: correct an earlier commit](https://kairos-2z6s.onrender.com/res/git-commit-fixup-full-en)
- [Git Commit Allow Empty: a commit with no changes](https://kairos-2z6s.onrender.com/res/git-commit-allow-empty-full-en)
- [Git Describe: name a commit from the nearest tag](https://kairos-2z6s.onrender.com/res/git-describe-full-en)
