How do I revert a Git repository to a previous commit?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reverting to a previous commit in Git can mean very different operations, and using the wrong one can disrupt collaborators or destroy local work. Some commands preserve history, while others rewrite branch pointers. The correct choice depends on whether the branch is shared and whether you want to keep existing commits visible.
Choose the Right Undo Strategy First
Before running any undo command, define your intent clearly.
- Undo bad changes on a shared branch while keeping history intact.
- Remove local unpublished commits from your branch tip.
- Inspect old code state without changing branch history.
Once intent is clear, command choice becomes straightforward.
Use git revert for Shared Branch Safety
git revert is usually the safest approach for shared branches. It creates a new commit that reverses the target commit patch, so existing history remains stable for everyone.
This avoids branch pointer rewrites and works cleanly with pull requests, CI traces, and release audit trails.
If you need to revert a range of commits as one change set:
This is useful when rollback should be documented as one operational action.
Use git reset for Local Unpublished History
git reset moves branch HEAD and is appropriate when commits are local and not shared.
Soft reset keeps changes staged:
Mixed reset keeps changes in working tree but unstaged:
Hard reset discards staged and working tree changes and moves branch to target commit:
Hard reset is destructive. Use it only when discarded changes are truly disposable or already backed up.
Inspect Older Commits Without Undoing Anything
Sometimes you only need to build or debug at an old commit. Use detached HEAD so branch tips remain unchanged.
Run your tests or inspection commands, then return to your branch.
This is the right tool for forensic debugging when no rollback is needed.
Recover Mistakes with Reflog
If you reset or checkout incorrectly, reflog often provides a fast recovery path because it records branch tip history locally.
After validating the rescue branch, you can restore branch tip if required.
Creating a temporary recovery branch first reduces risk while you inspect the intended restore point.
Handle Remote Divergence After Rewriting
If you rewrote local history and must update remote, push with lease protection.
Lease protection prevents silent overwrite when remote moved since your last fetch. If your branch is protected or shared broadly, prefer revert commits instead of force pushing rewritten history.
Practical Playbooks
Use the following patterns as operational defaults.
- Shared branch rollback:
git revert. - Local typo fix in latest commit:
git reset --mixed HEAD~1and recommit. - Private branch cleanup before first push:
git resetmodes as needed. - Old-state debugging: detached
HEADcheckout.
Having a standard playbook helps teams avoid high-risk ad hoc undo commands during incidents.
Common Pitfalls
- Using
git reset --hardon shared branches and breaking teammate history. - Confusing
revertandreset, then getting unexpected commit graphs. - Force pushing without lease protection and overwriting unseen remote work.
- Running destructive commands without
git statusandgit logpre-checks. - Forgetting reflog exists and assuming mistakes are unrecoverable.
Summary
- '
git revertis the safe default for shared branch rollback.' - '
git resetis for local unpublished history rewriting.' - Detached
HEADis for historical inspection without mutation. - Use reflog and rescue branches to recover from undo mistakes.
- Prefer
--force-with-leaseover plain force when rewritten history must be pushed. - Match undo method to collaboration model before running commands.

