Git
commit
SHA hash
version control
duplicate question

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.

Browse interview questions

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.

bash
git revert abc1234

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.

bash
git reset --hard abc1234

After that command:

  • 'HEAD points at abc1234'
  • 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:

bash
git reset --soft abc1234

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.

bash
git switch --detach abc1234

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.

bash
git switch main

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.

bash
git revert abc1234^..def5678

That tells Git to create revert commits for the range from abc1234 through def5678. If conflicts appear, resolve them, stage the fixes, and continue:

bash
git revert --continue

If you change your mind during a conflicted revert sequence, you can abort:

bash
git revert --abort

Choosing the Right Command

A practical rule is:

  • use git revert when history is shared and you want a safe undo
  • use git reset when you want to move the current branch pointer and history rewriting is acceptable
  • use git switch --detach when 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:

bash
1git switch main
2git pull
3git revert abc1234
4git push

If instead you are on a private branch and want to throw away later local commits:

bash
git switch feature-x
git reset --hard abc1234

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.