Revert to a commit by a SHA hash in Git?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Git, "revert to a commit by SHA" can mean several different operations. You might want to undo the effect of one commit, move your branch pointer back to an older commit, or inspect that historical state without changing the branch. The right command depends on whether you want to preserve public history or rewrite it.
Use git revert to Undo a Commit Safely
If the commit is already shared with other developers, git revert is usually the correct choice. It creates a new commit that applies the inverse of the target commit.
That does not move the branch backward. It adds a new commit on top of the current branch that cancels the effect of commit abc1234.
This is the safest option for shared branches because it preserves history instead of rewriting it.
Use git reset to Move the Branch Back
If what you really mean is "make my branch point at this old commit again," use git reset. This rewrites your current branch state.
After that command:
- '
HEADpoints atabc1234' - later commits are no longer on the current branch tip
- tracked working tree files are reset to that commit
This is appropriate only when you understand the consequences, especially if the branch has already been pushed.
If you want to move the branch pointer without discarding working tree changes, use a softer reset mode:
That keeps changes staged while moving HEAD.
Use git switch --detach to Inspect an Old Commit
Sometimes you do not want to revert or reset anything. You just want to look at a past snapshot or run the code from that point.
This checks out the commit in detached HEAD state. You can inspect files, run tests, or compare behavior without moving any branch pointer. When you are done, switch back to your branch.
This is the lowest-risk choice when the goal is investigation rather than history editing.
Revert a Range of Commits
If you need to undo several commits, you can revert them one by one or specify a range carefully.
That tells Git to create revert commits for the range from abc1234 through def5678. If conflicts appear, resolve them, stage the fixes, and continue:
If you change your mind during a conflicted revert sequence, you can abort:
Choosing the Right Command
A practical rule is:
- use
git revertwhen history is shared and you want a safe undo - use
git resetwhen you want to move the current branch pointer and history rewriting is acceptable - use
git switch --detachwhen you only need to inspect an old commit
Many Git problems come from using reset --hard when the real goal was just to undo one bad change safely.
Example Workflow
Suppose abc1234 introduced a bug on main and the commit is already on the remote. The correct fix is usually:
If instead you are on a private branch and want to throw away later local commits:
Those are very different operations, even though both might be described informally as "go back to this SHA."
Common Pitfalls
The biggest mistake is treating git revert and git reset --hard as interchangeable. They solve different problems. Another common issue is running reset --hard on a shared branch and forcing teammates to deal with rewritten history. Developers also forget that checking out a SHA in detached HEAD state does not move the branch. Merge commits need extra care when reverting, because Git may require a mainline parent choice. When in doubt, prefer the safer command that preserves history.
Summary
- "Revert to a SHA" is ambiguous, so choose the command based on your actual intent.
- Use
git revert <sha>to undo a commit safely on shared history. - Use
git reset --hard <sha>only when you intentionally want to move the current branch backward. - Use
git switch --detach <sha>to inspect an old commit without changing branch history. - Reverting ranges and merge commits can require extra care.
- If the branch is shared, preserving history is usually the safer choice.
Related reading
- Reverting to a previous commit in Git for visual studio 2012
- Reverting to a specific commit based on commit id with Git?
- Rollback a Git merge
- Rollback file to much earlier version using Git
- Rollback to an old Git commit in a public repo
- Rollback to an old Git commit in a public repo
- Ruby How to install a specific version of a ruby gem?
- Rubymine How to make Git ignore .idea files created by Rubymine
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.