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 g7h8i9j] 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..f4e5d6g
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.