# git revert: undo a commit by adding a new one

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-revert-full-en
Revert records a new commit that undoes the target change. TE-06-A covers the no-conflict case: one commit reverted cleanly and the file returns to its earlier content. TE-06-B covers revert with -n. One conflict is recorded in TE-06-C; resolving a conflict and continuing the revert are not tested on this page.

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-06-A, TE-06-B and TE-06-C. The expected non-zero exit is TE-06-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-06-A: git revert creates a new commit by default and undoes the target change

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 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git add f.txt; git commit -qm two
```

Command:

```bash
git revert --no-edit HEAD >/dev/null; echo commits=$(git log --oneline | wc -l); cat f.txt
```

Recorded output:

```
commits=3
one
```

### TE-06-B: revert with -n changes the index and the file but makes no 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 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git add f.txt; git commit -qm two
```

Command:

```bash
git revert -n HEAD; echo rc=$?; git status --short; echo commits=$(git log --oneline | wc -l); cat f.txt
```

Recorded output:

```
rc=0
M  f.txt
commits=2
one
```

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

### TE-06-C: expected conflict, non-zero exit: reverting an older commit whose lines were changed again stops with exit status 1

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 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git add f.txt; git commit -qm two
printf 'three\n' > f.txt; git add f.txt; git commit -qm three
```

The file `../out.txt` sits one level above the working folder, inside the same throwaway tree. It holds the output of the git command; the output block below shows only the counts and the status lines printed by the command.

Command:

```bash
git revert --no-edit HEAD~1 >../out.txt 2>&1; rc=$?; echo conflict_lines=$(grep -c CONFLICT ../out.txt); git status --short; exit $rc
```

Recorded output:

```
conflict_lines=1
UU f.txt
```

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

## Reading the recorded output

The output has two lines. commits=3 is the number of lines in git log --oneline after the revert. By the setup, there were two commits before it, one and two, so the revert added a third commit and did not remove either of the others. The second line is the content of f.txt, which is one, the text it held before the commit two changed it to two. The output of git revert itself was sent to /dev/null, so the run does not show the message of the new commit.

TE-06-B: git revert -n exited 0, git status --short printed M  f.txt, and the commit count stayed 2, so no third commit was made, while f.txt already reads one. TE-06-C: the revert exited with status 1. The output of the revert holds one line with the word CONFLICT, and git status --short lists UU f.txt. By the setup, commits two and three both changed f.txt, so reverting two collides with three.

## Limits

Merge commits need `-m`; a revert is not a history rewrite.

## Source

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

## Evidence

- `TE-06-A`: git revert creates a new commit by default and undoes the target change

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-06-A` | git revert creates a new commit by default and undoes the target change | 0 | PASS | `cc5e07981495` |
| `TE-06-B` | revert with -n changes the index and the file but makes no commit | 0 | PASS | `5fe6de49db53` |
| `TE-06-C` | expected conflict, non-zero exit: reverting an older commit whose lines were changed again stops with exit status 1 | 1 | PASS | `dd69c4abe091` |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Rm Cached: stop tracking a file and keep it on disk](https://kairos-2z6s.onrender.com/res/git-rm-cached-full-en)
- [Git Rev Parse: ask Git for a path or a commit id](https://kairos-2z6s.onrender.com/res/git-rev-parse-full-en)
- [Git Restore: unstage or discard a file](https://kairos-2z6s.onrender.com/res/git-restore-full-en)
