Undo pushed commit

2 snippets in Git

GitGit

Revert a Commit (Safe Undo)

Git · Undoing Changes
Syntax
git revert <commit>
Example
git revert a1b2c3d
git revert HEAD
Output
[main a7b8c9d] Revert "Add user authentication endpoint"
 2 files changed, 3 deletions(-), 45 insertions(-)

Note Revert creates a NEW commit that undoes the changes from the specified commit. Unlike reset, it doesn't rewrite history, making it safe for shared/public branches. Always prefer revert over reset for pushed commits.

Undo a Commit That's Already Pushed

Git · Common Mistakes & Recovery
Syntax
git revert <commit>
git push
Example
# The safe way - create a new commit that reverses the changes:
git revert a1b2c3d
git push origin main

# If you need to undo multiple commits:
git revert a1b2c3d..f4e5d6a
git push origin main

Note NEVER use git reset + force push on a shared branch. Use git revert instead, which creates a new commit that undoes the changes while preserving history. Everyone who pulled the original commit won't have conflicts.

Frequently asked questions

How do you undo pushed commit?
Git covers this with 2 copy-ready snippets on this page. The "Revert a Commit (Safe Undo)" snippet in Git uses `git revert <commit>`.
Which command does the Git example use?
The "Revert a Commit (Safe Undo)" snippet uses `git revert <commit>`, from the Undoing Changes section of the Git cheat sheet.
What other Git snippets are shown for "undo pushed commit"?
Besides "Revert a Commit (Safe Undo)", this page also shows "Undo a Commit That's Already Pushed".
Is there anything to watch out for?
Yes. For "Revert a Commit (Safe Undo)": Revert creates a NEW commit that undoes the changes from the specified commit. Unlike reset, it doesn't rewrite history, making it safe for shared/public branches. Always prefer revert over reset for pushed commits.