Revert a commit with git revert

Description: The commit “Add debug page” was a mistake, and it is already pushed. Undo it without changing the history.

Hint:

  • Use git revert with --no-edit to accept the default message.

Solution

Terminal output

$ git revert HEAD~1 –no-edit
[main a51ba50] Revert "Add debug page"
Date: Sat Jan 10 11:31:00 2026 +0000
1 file changed, 1 deletion(-)
delete mode 100644 debug.txt
$ git log –oneline
a51ba50 (HEAD -> main) Revert "Add debug page"
27e27e3 Add about page
f5f5ec5 Add debug page
95d0194 Add home page
$ ls
about.txt
home.txt

How it works

  • Git made a new commit which removes debug.txt.
  • The old commits are still in the history, so it is safe for shared branches.

Read Git Revert.


Learn more about the similar topics:
Tutorials
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?