How to Revert a Commit in Git After a Push

Sometimes after pushing a commit to a remote repository like GitHub, you realize something was wrong — maybe a bug slipped in, or you committed the wrong files. Luckily, Git provides ways to undo or revert commits even after they’ve been pushed.

In this guide, you’ll learn the safest ways to undo a commit that has already been pushed, without breaking your team’s workflow.


🔍 Should You Revert or Reset?

  • Revert: Safely undo a commit by creating a new commit that undoes the changes.
  • Reset: Changes your branch history, which can cause problems if others already pulled your commits.

Tip: If the commit has been pushed and shared, use revert to avoid rewriting history.


✅ How to Revert a Commit After Push

1. Identify the commit hash you want to revert

Run:

git log --oneline

This shows recent commits. Copy the commit hash (e.g., a1b2c3d).

2. Run the revert command

git revert <commit-hash>

Example:

git revert a1b2c3d
  • This creates a new commit that reverses the changes introduced by the original commit.
  • Git will open your default editor to confirm or modify the commit message.

3. Push the revert commit to remote

git push origin <branch-name>

Example:

git push origin main

🛠️ Revert Multiple Commits

To revert a range of commits:

git revert <oldest-commit>^..<newest-commit>

Or revert commits one by one from newest to oldest.


⚠️ Avoid Using git reset After Push (Unless You Know What You’re Doing)

git reset rewrites commit history and can cause serious issues if others have based work on those commits.

If you must rewrite history after pushing, coordinate with your team and force push carefully:

git reset --hard <commit-hash>
git push origin <branch-name> --force

🧠 Summary of Commands

TaskCommand
View recent commitsgit log --oneline
Revert a commitgit revert <commit-hash>
Push revert commitgit push origin <branch>
Reset branch (use with care)git reset --hard <commit-hash> + force push

🧩 Final Thoughts

Reverting commits after pushing is a common task and Git’s revert command lets you do it safely by creating a new commit that undoes changes without rewriting history. This approach keeps your project history clean and collaboration smooth.

Sharing Is Caring:

Leave a Comment