Git
Version Control
Rollback
Commits
Git Commands

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.

Browse interview questions

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.

bash
git log --oneline --decorate

That prints a short history such as:

text
9f31b7a fix payment retry logic
2c48d9e add webhook handler
8e6c3aa initial checkout flow

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.

bash
git checkout 2c48d9e

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.

bash
git switch -c investigate-old-state

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.

bash
git reset --soft 2c48d9e

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.

bash
git reset 2c48d9e

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.

bash
git reset --hard 2c48d9e

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.

bash
git revert 9f31b7a

If you need to undo a range of commits, you can revert multiple commits one at a time or use a range carefully.

bash
git revert --no-commit 2c48d9e..9f31b7a
git commit -m "Revert recent changes"

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.

bash
git push --force-with-lease origin my-branch

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 --oneline to find the target commit.
  • Use git checkout when you only want to inspect old state.
  • Use git reset when you want to move the current branch pointer.
  • Use git revert when changes are already shared and history should stay intact.
  • Treat git reset --hard with care because it discards tracked local changes.
  • Use git push --force-with-lease instead of plain force push when rewriting remote history.

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.