Git in a Minute · intermediate

Git in a Minute: Recovery

Git recovery tools—reflog, reset, revert—help undo agent mistakes without panicking.

Last verified: 2026-09-06

In one sentence

Recovery is how you return a repository to a known-good state after bad commits, resets, or lost refs—without guessing.

Why it matters

Agents make mistakes at machine speed. Knowing recovery paths turns incidents into short detours.

How it works

Prefer this order:

  1. git status / git diff — understand the damage;
  2. git revert — add an undo commit on shared branches;
  3. git reset — move a private branch pointer (careful);
  4. git reflog — find commits that seem “lost” after reset/rebase.

Never rewrite public/shared history casually. Prefer revert on main.

Example

git reflog
git reset --hard HEAD@{1}   # private branch only, after verifying the entry

Or on a shared branch:

git revert <bad-commit-sha>

Agentic coding use

Teach recovery as part of agent operations. Pair agents with worktrees so a blown directory does not take down your primary checkout.

Watch out

reset --hard discards uncommitted work. Check git status first. Also remember remotes: a local reset does not undo a push already fetched by others.

Sources