How can I rollback a git repository to a specific commit?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Rolling a Git repository back to a specific commit can mean very different things depending on whether you want to inspect old code, undo local work, or rewrite branch history. The safe command for one situation can be the wrong command for another, so the first step is understanding the effect you actually want.
Find the Commit You Want
Before changing anything, identify the target commit.
That prints a short history such as:
Assume you want to go back to 2c48d9e. From here, you have three common choices: checkout, reset, or revert.
Inspect an Older Commit with Checkout
If you only want to view or test the repository at an older point in time, use git checkout with the commit hash.
This puts Git into a detached HEAD state. Your files now match that commit, but your branch pointer does not move.
That is useful for debugging or comparing behavior. If you decide to keep working from there, create a branch.
Use this approach when you do not want to alter the history of your current branch.
Move the Branch with Reset
If you want your current branch to point back to an earlier commit, use git reset. This changes history for the current branch.
Soft Reset
A soft reset moves the branch but keeps changes staged.
Use this when you want to rebuild commits while keeping the changes ready to recommit.
Mixed Reset
A mixed reset is the default. It moves the branch and keeps changes in the working tree but unstaged.
This is useful when you want to keep the file changes but reorganize them.
Hard Reset
A hard reset moves the branch and discards tracked working tree changes.
This is the strongest option. It is appropriate when you are certain you want the branch and local files to match that exact commit.
Undo Shared History with Revert
If commits have already been pushed and other people may depend on them, git revert is usually safer than git reset. Revert creates a new commit that undoes an earlier one without rewriting branch history.
If you need to undo a range of commits, you can revert multiple commits one at a time or use a range carefully.
This approach keeps the history honest and is usually the right answer for shared branches such as main.
What to Do After a Reset
If you reset a local branch that has already been pushed, your local history no longer matches the remote branch. Pushing may require a force push.
Use --force-with-lease rather than plain --force because it adds a safety check. It prevents you from overwriting remote updates that you have not seen.
If the branch is shared, coordinate with collaborators before rewriting history.
Common Pitfalls
The biggest mistake is using git reset --hard when you only meant to inspect an old commit. That command can discard local work permanently if it is not committed somewhere else.
Another common issue is rewriting the history of a shared branch. A hard reset followed by a force push can break teammates' local clones and create confusing merge situations.
Detached HEAD also surprises many developers. After git checkout to a commit, new commits are not attached to a normal branch unless you create one.
Finally, do not confuse undoing files with undoing history. git revert preserves history and adds a new reversal commit, while git reset moves the branch itself.
Summary
- Use
git log --onelineto find the target commit. - Use
git checkoutwhen you only want to inspect old state. - Use
git resetwhen you want to move the current branch pointer. - Use
git revertwhen changes are already shared and history should stay intact. - Treat
git reset --hardwith care because it discards tracked local changes. - Use
git push --force-with-leaseinstead of plain force push when rewriting remote history.
Related reading
- How can I save username and password in Git?
- How can I search Git branches for a file or directory?
- How can I see the changes in a Git commit?
- How can I see the differences between two branches?
- How can I see the size of a GitHub repository before cloning it?
- How can I see what has changed in a file before committing to git?
- How can I see what I am about to push with git?
- How can I see which Git branches are tracking which remote / upstream branch?
.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.