git
git commit
git push
version control
undo changes

How can I undo a git commit locally and on a remote after git push

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Undoing a commit after git push depends on whether the commit is already shared and whether you can safely rewrite history. There is no single command for every case. The right choice is between git revert for safe public history and git reset plus force push for controlled history rewrite.

Option One Revert for Shared Branches

If others may have pulled the commit, use git revert. It creates a new commit that undoes changes without rewriting existing history.

bash
1git checkout main
2git pull --ff-only
3git revert <commit_sha>
4git push origin main

This is the safest approach for protected branches and collaborative repositories. Audit trails remain clear and no teammate history is invalidated.

To revert multiple commits, either run multiple revert commands or revert a range with no commit flag then create one combined revert commit.

bash
git revert --no-commit old_sha^..new_sha
git commit -m "Revert broken release changes"
git push origin main

Option Two Reset and Force Push for Personal Branches

If branch is private or coordinated, you can remove commits from history with reset and force push.

bash
1git checkout feature/login
2git log --oneline -5
3git reset --hard HEAD~1
4git push --force-with-lease origin feature/login

Use --force-with-lease rather than plain force. It aborts if remote has unexpected commits, reducing risk of accidental overwrite.

Soft reset keeps file changes staged, useful when commit message was wrong but content should stay:

bash
git reset --soft HEAD~1

Mixed reset keeps changes unstaged:

bash
git reset --mixed HEAD~1

Recovery When You Reset Too Far

Git reflog can recover dropped commits as long as they have not been garbage collected.

bash
git reflog
git reset --hard <reflog_sha>

This is a critical safety net during cleanup operations.

Team Policy and Branch Protection

Before rewriting remote history, confirm branch policy. Many repos protect main branch and disallow force pushes. In that case revert is the only supported method.

For incident response, document the exact command sequence used to undo changes. This helps reviewers and avoids confusion when multiple rollback attempts happen quickly.

Undoing a Merge Commit Safely

Merge commits need extra care because they have multiple parents. For shared branches, use revert with explicit mainline parent to preserve history correctly.

bash
git log --oneline --graph -10
git revert -m 1 <merge_commit_sha>
git push origin main

The -m 1 value tells Git which parent represents the mainline branch. Choose wrong parent and you may revert unexpected content.

Operational Rollback Workflow

During production incidents, speed matters but so does traceability. A good rollback workflow is:

  1. Tag current remote head before undo.
  2. Apply revert or controlled reset.
  3. Push and verify CI status.
  4. Post rollback commit hash in incident channel.
bash
git tag rollback-snapshot-2026-03-04
git push origin rollback-snapshot-2026-03-04

This leaves a clear recovery point if the rollback itself needs to be reverted later.

Preventive Workflow Before Push

The easiest rollback is the one you never need. Before pushing, inspect the last commit and run quick tests. If commit content is wrong but not yet pushed, amend locally instead of creating extra fix commits.

bash
git show --stat --oneline HEAD
git commit --amend

Small pre push checks reduce emergency rollbacks and keep branch history cleaner.

Common Pitfalls

  • Force pushing on shared main branches without team coordination.
  • Using git reset --hard before saving local uncommitted work.
  • Forgetting to fetch and verify remote state before rewrite.
  • Reverting merge commits without understanding parent selection.
  • Assuming bad commit is unrecoverable without checking reflog.

Summary

  • Use revert for shared history that should remain stable.
  • Use reset plus --force-with-lease for controlled private branch rewrites.
  • Choose reset mode based on whether you keep working tree changes.
  • Use reflog to recover commits after accidental resets.
  • Align rollback method with team branch protection rules.

Course illustration
Course illustration

All Rights Reserved.