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.
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:
Suppose the target commit is:
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.
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:
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.
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:
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:
Moves the branch but keeps changes staged.
Moves the branch and unstages the changes, but keeps them in the working tree.
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.
Then restore the previous state if needed:
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 --hardon 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
HEADis 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 --detachto inspect an old commit safely. - Use
git resetto move your branch back to a specific commit. - Use
git revertto 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
- 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
- Run git pull over all subdirectories
- running git clone against AWS CodeCommits gets me a 403 error
.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.