Skip to content
gitrep
Library

a3f9c21guide1 min read

Undo almost anything in Git

A calm guide to reflog, reset, revert, and restore — and how to choose the right one when something goes wrong.

Published Updated

Git rarely loses work. Almost every mistake — a bad rebase, a reset to the wrong commit, a deleted branch — leaves a trail you can follow back. The trick is knowing which command matches the situation.

Start with the reflog

The reflog records every position HEAD has pointed to, even for commits no branch references anymore. When in doubt, look here first.

sh
git reflog --date=relative
# a3f9c21 HEAD@{0}: rebase (finish): returning to refs/heads/main
# 7be41d0 HEAD@{1}: commit: Add search filters

Each line is a safe point. To return to one, create a branch from it before doing anything else.

sh
git branch rescue HEAD@{1}

Choosing between reset, revert, and restore

  • git restore <file> discards uncommitted changes to a file. Nothing in history moves.
  • git reset --soft <ref> moves the branch back but keeps your changes staged — ideal for rewriting the last few commits.
  • git revert <ref> adds a new commit that undoes an old one. Use it on shared branches, where history must not change.

Recovering a deleted branch

A deleted branch is only a deleted pointer. Find its last commit in the reflog and point a new branch at it.

sh
git reflog | grep 'checkout: moving from feature/search'
git branch feature/search 7be41d0
  1. Interactive rebase without fearSquash, reorder, and reword commits before review, with a safety net in place.Guide