Undoing Without Rewriting
Amending replaces a commit, which is fine while nobody else has it. Once a commit is shared, or when the bad commit is several commits back, you need a way to undo it that leaves the existing history alone.
git revert does that. It works out the opposite of a commit's changes and
records them as a new commit. Lines the commit added are removed, lines it
removed come back, and every other commit stays exactly as it was.
What the History Looks Like

In the diagram, "Raise timeout to 300" was a mistake, but two good commits came after it. After the revert:
- The bad commit is still in the history. Anyone reading the log can see what happened and when it was undone.
- The later commits, "Add contact page" and "Use dark theme", are untouched, and so are their changes.
- A new commit at the top holds the opposite change, setting the timeout back.
Because nothing is removed or replaced, a revert is safe for commits other people already have. It is just one more commit for them to receive.
Running a Revert
Name the commit to undo, by ID or by a name such as HEAD~2:
git revert --no-edit <commit>
Git writes a message for the new commit: Revert "<original summary>", with a
body saying This reverts commit <full ID>. Without --no-edit, Git opens
your editor with that message so you can add the reason for the revert; save
and close the editor to finish.
Start with a clean git status. If you have uncommitted edits to a file the
revert needs to change, Git stops with Your local changes to the following files would be overwritten and fatal: revert failed, and changes nothing.
When a Revert Conflicts
A revert can only undo lines that still look the way the commit left them. If a later commit changed the same lines again, Git cannot tell what the result should be, so it stops with a conflict and asks you to decide.
In a conflicted file, Git marks the disputed lines:
<<<<<<< HEAD
retries = 5 # attempts before giving up
=======
>>>>>>> parent of 6569da9 (Add retry limit)
- Between
<<<<<<< HEADand=======is what the file contains now. - Between
=======and>>>>>>>is what the revert wants, here nothing, because reverting the commit that added the line means removing it.
To resolve it, edit the file so it reads the way it should, with all three marker lines gone, then mark it resolved and finish:
git add <file>
git revert --continue
git revert --continue opens your editor with the prepared message; save and
close it. If you would rather not continue, git revert --abort puts
everything back as it was before the revert started. The branching module
covers conflicts in much more depth.
Exercise: Undo a Commit in the Middle of a History
A settings file had its timeout raised by mistake, and two good commits came after. You will revert the bad commit, then revert a second one that conflicts.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It makes four commits:
mkdir -p ~/git-practice/revert-practice cd ~/git-practice/revert-practice git init -b main cat > config.txt <<'EOF' # App settings theme = light language = en timeout = 30 EOF git add config.txt git commit -m "Add app settings" cat > config.txt <<'EOF' # App settings theme = light language = en timeout = 300 EOF git commit -am "Raise timeout to 300" cat > contact.md <<'EOF' # Contact Email the team with questions. EOF git add contact.md git commit -m "Add contact page" cat > config.txt <<'EOF' # App settings theme = dark language = en timeout = 300 EOF git commit -am "Use dark theme" -
Find the bad commit and confirm what it changed:
git log --oneline git show HEAD~2"Raise timeout to 300" is two steps back from
HEAD, and its changes show-timeout = 30and+timeout = 300. -
Revert it:
git revert --no-edit HEAD~2 -
Check the result:
git log --oneline cat config.txt ls git showThe log has five commits, with
Revert "Raise timeout to 300"on top and the bad commit still below.config.txthastimeout = 30and still hastheme = darkfrom the later commit.contact.mdis still there.git showdisplays the opposite change,-timeout = 300and+timeout = 30, under the messageThis reverts commitfollowed by the bad commit's full ID. -
Add a setting, then change the same line again in a second commit:
cat >> config.txt <<'EOF' retries = 5 EOF git commit -am "Add retry limit" cat > config.txt <<'EOF' # App settings theme = dark language = en timeout = 30 retries = 5 # attempts before giving up EOF git commit -am "Explain the retry limit" -
The team decides there should be no retry limit. Revert the commit that added it, which is one step back:
git revert --no-edit HEAD~1Git reports
CONFLICT (content): Merge conflict in config.txtanderror: could not revert. Look at the state:git status cat config.txtgit statussaysYou are currently reverting commitand listsboth modified: config.txt. The file ends with the conflict markers shown in the When a Revert Conflicts section. The later commit changed the line the revert wants to remove. -
Decide: the setting should go, comment and all. Write the file without the retry line and without the markers, then finish the revert. Your editor opens with the message
Revert "Add retry limit"; save and close it.cat > config.txt <<'EOF' # App settings theme = dark language = en timeout = 30 EOF git add config.txt git revert --continue -
Check:
git log --oneline -3 cat config.txtThe newest commit is
Revert "Add retry limit", andconfig.txthas no retry line.
Success: both bad changes are undone by new revert commits, the later good
changes are still in place, and git log still shows every original commit.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
An editor opened during git revert | You left out --no-edit | Save and close it to accept the message |
| The wrong commit was reverted | HEAD~n counted from the wrong commit | git revert --no-edit HEAD reverts the revert and brings the change back; then revert the right commit |
Your local changes to the following files would be overwritten | Uncommitted edits to a file the revert changes | Commit or discard them, then revert again |
error: no cherry-pick or revert in progress | There was no conflict, or the revert already finished | Check git log --oneline; the revert commit is probably there |
| Conflict markers left in the committed file | The file was added before all three marker lines were removed | Fix the file and commit the correction |
-
Clean up:
cd ~/git-practice ls rm -rf revert-practice ls
Independent task: before cleaning up, the contact page should not have been
published. Undo the commit "Add contact page" without changing any other
commit. Find its ID with git log --oneline rather than counting steps from
HEAD. Then check that contact.md is gone and that "Add contact page" is
still in the log.
Check Your Understanding
- After
git revert, is the reverted commit still ingit log? Why does that make revert safe for shared commits? - A colleague suggests deleting the bad commit from the history instead of reverting it, although the team already has it. What problem would that cause?
Answer and Reasoning
Independent task
git log --oneline
git revert --no-edit <ID>
Use the ID shown next to "Add contact page" in your log. The revert commit
removes contact.md, so ls no longer lists it, and the newest commit is
Revert "Add contact page". git log --oneline still lists "Add contact
page" further down: reverting never removes the original commit. No conflict
occurs, because no later commit changed contact.md.
Understanding check
- Yes. A revert adds a new commit containing the opposite change and leaves the original where it was. Because nothing in the existing history changes, everyone who already has those commits can simply receive the new one.
- Deleting a commit others have means rewriting the history after it, so your history and your teammates' would no longer match, and their copies would still contain the bad commit. A revert undoes the change while keeping one shared history. The next lesson shows the commands that move a branch back and when they are appropriate.