How to Revert to a Previous Commit in Git (Safely)

By Marko Denic
Contents
  1. Find the commit hash
  2. Pushed commits: git revert
  3. Undo one commit
  4. Undo several commits
  5. Revert a merge commit
  6. Local commits: git reset
  7. --soft: keep the changes staged
  8. --mixed (the default): keep the changes unstaged
  9. --hard: throw everything away
  10. Already pushed and you still want reset?
  11. Other ways to go back
  12. Look at an old commit without changing anything
  13. Restore a single file from an old commit
  14. Undo a mistake with reflog
  15. FAQ

There are two ways to go back to a previous commit in Git, and picking the right one depends on one question: have you pushed the commits you want to undo?

Situation Command What happens
Already pushed / shared git revert <hash> Adds a new commit that undoes the changes. History is kept.
Only on your machine git reset <hash> Moves the branch back. The later commits disappear from the branch.
Just want to look git switch --detach <hash> Checks out the old commit without changing any branch.

First, find the commit you want to go back to.

Find the commit hash

Each commit has a hash; the first 7 characters are enough to identify it.

bash
git log --oneline

# a1b2c3d (HEAD -> main) Break the header
# e4f5a6b Add pricing page
# 9c8d7e6 Fix footer links   ← the last good commit

Pushed commits: git revert

Undo one commit

git revert creates a new commit with the opposite changes. Nothing is deleted, so it’s safe on shared branches: your teammates just pull the new commit.

bash
git revert a1b2c3d
git push

Undo several commits

Revert a range to go back to the state of an older commit. --no-commit stages all the reverts together so you can commit them as one.

bash
# Undo everything after 9c8d7e6, as a single commit
git revert --no-commit 9c8d7e6..HEAD
git commit -m "Revert to 9c8d7e6: header broke production"

Revert a merge commit

A merge commit has two parents, so Git needs to know which side to keep. -m 1 keeps the branch you merged into (usually main) and undoes what was merged.

bash
git revert -m 1 <merge-commit-hash>

Local commits: git reset

git reset moves your branch pointer back to an older commit. The three modes decide what happens to the changes from the commits you “removed”.

--soft: keep the changes staged

Undo the commits but keep all their changes staged, ready to commit again. Perfect for squashing your last few commits into one.

bash
git reset --soft HEAD~3      # undo the last 3 commits
git commit -m "One clean commit"

--mixed (the default): keep the changes unstaged

Undo the commits and unstage the changes, but leave them in your files.

bash
git reset 9c8d7e6

--hard: throw everything away

Your branch and your files go back to exactly how they were at that commit. Uncommitted changes are lost for good. Run git status first, and stash anything you want to keep.

bash
git stash            # save uncommitted work, just in case
git reset --hard 9c8d7e6

Already pushed and you still want reset?

After resetting a branch you’ve pushed, a normal push is rejected. Force-pushing rewrites history for everyone, so only do it on your own branches. --force-with-lease refuses to overwrite commits someone else pushed in the meantime.

bash
git push --force-with-lease

Other ways to go back

Look at an old commit without changing anything

Check out the commit in “detached HEAD” mode to look around or run the old version. Switch back to your branch when you’re done.

bash
git switch --detach 9c8d7e6
# ...look, test...
git switch main

Restore a single file from an old commit

Often only one file is broken. Bring back just that file, then commit it.

bash
git restore --source 9c8d7e6 -- src/header.js

# Older syntax
git checkout 9c8d7e6 -- src/header.js

Undo a mistake with reflog

Reset too far? Git remembers every position your branch has been at for weeks. git reflog lists them; reset back to the one before your mistake.

bash
git reflog
# 9c8d7e6 HEAD@{0}: reset: moving to 9c8d7e6
# a1b2c3d HEAD@{1}: commit: Break the header   ← before the reset

git reset --hard HEAD@{1}

FAQ

What’s the difference between git revert and git reset?

git revert adds a new commit that undoes changes, keeping history intact, so it’s safe for pushed work. git reset moves the branch back and removes commits from it, which rewrites history.

How do I undo the last commit but keep my changes?

git reset --soft HEAD~1 keeps them staged, git reset HEAD~1 keeps them unstaged.

How do I go back to a previous commit and delete everything after it?

git reset --hard <hash>, then git push --force-with-lease if the branch was pushed. Only do this on branches nobody else uses.

Can I recover commits after git reset –hard?

Usually yes: find them with git reflog and reset back. Uncommitted changes, however, can’t be recovered.