[Git] Undo a commit with git revert

Created on November 28, 2026

You can use git revert to create a new commit that undoes the changes introduced by a specified commit. It is useful when you want to back out a change without rewriting history, such as after the commit has been pushed to a remote. This differs from discarding working tree changes with git restore or moving history with git reset.

Revert the latest commit

Pass the commit hash you want to undo; for the most recent commit you can use HEAD.

git revert HEAD --no-edit

For example, if the previous commit added a line to foo.txt, after the revert the history gains a commit titled Revert "..." and foo.txt returns to its earlier contents.

a71302d Revert "add world"
3f84e43 add world
49acdaa initial
hello

Without --no-edit, Git opens an editor so you can change the revert commit message. Add --no-edit when you want to keep the default message.

Revert a specific commit

To undo an older commit, pass its hash.

git revert 3f84e43

References

Git