Git
Version Control
Commit Revert
Git Commands
Software Development

Reverting to a specific commit based on commit id with Git?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

"Go back to a specific commit" can mean three different things in Git: inspect an old commit, move your branch pointer back to it, or create a new commit that undoes later changes. The right command depends on which of those outcomes you actually want.

First Find the Commit ID

Start by locating the commit you care about:

bash
git log --oneline

Suppose the target commit is:

text
abc1234 Fix authentication bug

Now decide what you want the repository to do with that commit.

Option 1: Inspect the Old Commit Without Changing the Branch

If you only want to look at the old state, check out the commit in detached HEAD mode.

bash
git switch --detach abc1234

This lets you inspect files, build the code, or compare behavior without changing the branch tip.

If you decide the old state deserves a new branch:

bash
git switch -c investigate-old-state

This is the safest option when you only want to inspect or test.

Option 2: Move the Current Branch Back to That Commit

If your goal is to discard later commits locally and make the branch point at the old commit again, use reset.

bash
git reset --hard abc1234

This changes:

  • 'HEAD'
  • the current branch pointer
  • the working tree

Use this only when you really want to abandon the commits after abc1234, or when you are prepared to force-push later if the branch is already shared.

If you want to keep the file changes from later commits but remove the commit history, use a softer reset mode instead.

Option 3: Undo Later Commits with New Revert Commits

If the branch history is already shared, resetting may be the wrong tool. In that case, git revert is usually safer because it preserves history and adds new commits that undo earlier changes.

For a single commit:

bash
git revert abc1234

If the real goal is "make the branch content look like that older commit again," you may need to revert the range of commits that happened after it rather than reverting the target commit itself.

That is why people often confuse revert and reset. They solve different problems.

A Practical Rule for Choosing the Command

Use this rule of thumb:

  • want to inspect old code: git switch --detach <commit>
  • want to move the branch back locally: git reset
  • want to undo changes safely on a shared branch: git revert

The commit ID is the same input, but the outcome is very different.

Soft, Mixed, and Hard Reset

If you choose reset, know the modes:

bash
git reset --soft abc1234

Moves the branch but keeps changes staged.

bash
git reset --mixed abc1234

Moves the branch and unstages the changes, but keeps them in the working tree.

bash
git reset --hard abc1234

Moves the branch and overwrites the working tree to match the target commit exactly.

The more destructive the mode, the more careful you need to be.

Reflog Can Rescue Mistakes

If you reset to the wrong commit, Git usually still knows where HEAD was.

bash
git reflog

Then restore the previous state if needed:

bash
git reset --hard HEAD@{1}

This is one of the most useful safety nets in Git, especially after accidental history moves.

Shared Branches Need Extra Care

If you reset a branch that others already pulled, you are rewriting published history. That usually means a force-push and team coordination.

In those cases, reverting is often safer because it creates a clear history of what was undone without invalidating everyone else's branch pointers.

So the right answer depends not only on the commit ID, but also on whether the branch is local-only or already shared.

Common Pitfalls

  • Saying "revert to a commit" when the real intent is inspect, reset, or revert, all of which are different.
  • Using git reset --hard on a shared branch without understanding that history rewrite may follow.
  • Reverting the target commit when the real goal was to undo all commits after it.
  • Forgetting that detached HEAD is fine for inspection but not for normal branch development unless you create a branch.
  • Assuming a bad reset is unrecoverable without checking git reflog.

Summary

  • A commit ID can be used for inspection, branch reset, or history-preserving reversion, depending on the command.
  • Use git switch --detach to inspect an old commit safely.
  • Use git reset to move your branch back to a specific commit.
  • Use git revert to undo changes on a shared branch without rewriting history.
  • Always choose the command based on the desired outcome, not just on the phrase "go back to this commit."

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.