git reflog: recover the branch or commit an AI agent deleted

tutorialgitworkflow

TL;DR: Git keeps a local log of where every ref has pointed — the reflog — and it is the fastest way to get back work that an agent deleted. The trap is that deleting a branch deletes that branch’s reflog too: git reflog show <branch> fails with fatal: ambiguous argument '<branch>': unknown revision or path not in the working tree, and git reflog --all stops listing it. The commits are still on disk as unreachable objects, and git fsck --unreachable --no-reflogs finds them — it printed unreachable commit 10a2fa73b5619f280e022583a28321cddc36dfa7 in the reproduction I ran today on Git 2.47.3. Recover with git branch <name> <sha>. The clock on this is gc.reflogExpireUnreachable, which defaults to 30 days.

What you’ll be able to do:

  • Recover a branch an agent deleted, with the exact four commands and the exact output.
  • Understand why git reflog show <deleted-branch> fails and what to run instead.
  • Recover a commit that a reset --hard or an --amend swallowed.
  • Know exactly how long the recovery window is, and why a fresh clone has no safety net at all.

Honest take: every command and every line of output in this article was reproduced on November 19, 2026 on Git 2.47.3 in a throwaway repository on Linux. The SHA values are from that reproduction and will not match your repo. This is a recovery procedure, not a backup strategy — and it does not survive git gc --prune=now.

What the reflog actually is

The reflog is not a commit graph, and it is not shared. It is a plain text log, one file per ref, under .git/logs/. HEAD has one (logs/HEAD), every branch has one (logs/refs/heads/<branch>), and each line records the old SHA, the new SHA, who did it, when, and why:

10a2fa7 HEAD@{2026-11-19 09:25:10 +0800}: commit: feat: agent change
91c0a2a HEAD@{2026-11-19 09:25:10 +0800}: checkout: moving from main to feature/agent-work

The reason field is the part humans find useful, because it is Git telling you what it did, in Git’s own words: commit, commit (amend), checkout: moving from ..., reset: moving to ..., branch: Created from ..., rebase (start), merge ....

It is enabled by default in any non-bare repository — the core.logAllRefUpdates setting turns the reflog on for HEAD, branches, remotes and the stash when it is true — and it is a local record. A fresh git clone has no reflog history from the machine you cloned from. If the agent worked in a scratch clone and you deleted that clone, the reflog is gone with it.

The reproduction

Here is the whole scenario, run today on Git 2.47.3:

$ git init -q -b main .
$ echo one > a.txt && git add a.txt && git commit -qm "feat: add one"
$ git checkout -qb feature/agent-work
$ echo two >> a.txt && git commit -qam "feat: agent change"
$ git rev-parse HEAD
10a2fa73b5619f280e022583a28321cddc36dfa7
$ git checkout -q main
$ git branch -D feature/agent-work
Deleted branch feature/agent-work (was 10a2fa7).

That is the accident: an agent finishes a task on a scratch branch, decides the branch is noise, and deletes it. The commit exists on exactly one branch and that branch is now gone.

Step 1 — look at what is still tracked

$ git reflog --all --date=iso
91c0a2a refs/heads/main@{2026-11-19 09:25:10 +0800}: commit (initial): feat: add one
91c0a2a HEAD@{2026-11-19 09:25:10 +0800}: checkout: moving from feature/agent-work to main
10a2fa7 HEAD@{2026-11-19 09:25:10 +0800}: commit: feat: agent change
91c0a2a HEAD@{2026-11-19 09:25:10 +0800}: checkout: moving from main to feature/agent-work
91c0a2a HEAD@{2026-11-19 09:25:10 +0800}: commit (initial): feat: add one

Two things to read here. First, refs/heads/feature/agent-work is gone from the list — the branch and its reflog were deleted together, which is exactly the trap. Second, the commit 10a2fa7 is still visible in the HEAD reflog, because in this reproduction the branch had been checked out and HEAD moves were logged separately.

That second point is luck, not a rule. If the agent created and deleted the branch without ever checking it out, HEAD never moved, and the commit appears nowhere in git reflog.

Step 2 — ask Git what is unreachable

$ git fsck --unreachable --no-reflogs --no-progress
unreachable blob 814f4a422927b82f5f8a43f8fab6d3839e3983f2
unreachable tree 6218aaa5fc1a58f5b32cbee55bc0cb0954022787
unreachable commit 10a2fa73b5619f280e022583a28321cddc36dfa7

--no-reflogs matters. Without it, fsck treats anything still named by a reflog as reachable and prints nothing — which is the second way this recovery can look like a dead end. With --no-reflogs, Git ignores the reflog and reports the objects that no ref points at any more. The unreachable commit line is your commit.

If you are not sure which line is the one you want, the commit message is in the object, so check it before touching anything:

$ git show --stat --oneline 10a2fa7
10a2fa7 feat: agent change
 a.txt | 1 +

Step 3 — let Git write the pointer down for you

$ git fsck --lost-found --no-progress
dangling commit 10a2fa73b5619f280e022583a28321cddc36dfa7
$ ls .git/lost-found/commit/
10a2fa73b5619f280e022583a28321cddc36dfa7

--lost-found writes each dangling object to .git/lost-found/, so you get a file you can copy from instead of a SHA you have to trust your memory for. Note the wording difference: unreachable commit (from --unreachable) versus dangling commit (from --lost-found) — both mean “no ref points at this any more”, and both print the same SHA here.

Step 4 — put a branch back on it

$ git branch feature/agent-work-recovered 10a2fa73b5619f280e022583a28321cddc36dfa7
$ git branch -v
  feature/agent-work-recovered 10a2fa7 feat: agent change
* main                          91c0a2a feat: add one
$ git log --oneline -2 feature/agent-work-recovered
10a2fa7 feat: agent change
91c0a2a feat: add one

The branch is back, with its parent, and the commit is reachable again. There is no “restore” magic — a branch is just a movable pointer, and you moved it.

The other three ways agents lose work

The same mechanism covers the other common accidents, and in each case the HEAD reflog is the fast path:

What the agent didWhere the commit still isHow to get it back
git reset --hard <earlier> on the current branchHEAD reflog, entry reset: moving to ...git reset --hard HEAD@{2}
git commit --amend that swallowed the previous commitHEAD reflog, entry commit (amend)git show HEAD@{1} to inspect, then git reset --soft HEAD@{1}
git checkout -- <file> that overwrote uncommitted workNot in the reflog at all — it was never committedNothing in Git recovers it; this is what a stash or a worktree is for

The third row is the one to internalise. The reflog records where refs pointed. It cannot record the contents of a file that was never committed. If an agent overwrites your working tree with git checkout -- or git restore, the only recovery is the other file: git fsck will not find it, because it was never an object.

How long you have

The reflog is not kept forever. From the git-config documentation:

  • gc.reflogExpire — “git reflog expire removes reflog entries older than this time; defaults to 90 days”.
  • gc.reflogExpireUnreachable — “git reflog expire removes reflog entries older than this time and are not reachable from the current tip; defaults to 30 days”.

So the entries that matter most for a deleted branch — the ones that are no longer reachable from anything — expire after 30 days by default. git gc runs git reflog expire as part of its work, so the window is enforced by ordinary maintenance, not by an emergency.

If you want a longer window on a repository where agents work:

$ git config gc.reflogExpireUnreachable 180d

What this does not save you from

  • A fresh clone. The reflog is per-machine. Cloning a repo gives you the branches and commits, none of the local history of how they got there.
  • Force-pushed branches. If the deleted commit was never pushed, it only existed locally, and only the local object store has it. If it was pushed and then the branch was deleted on the remote, the remote’s garbage collection decides how long it stays fetchable — and that is not a promise you should build a workflow on.
  • A git gc --prune=now run. Pruning drops unreachable objects immediately. It is a legitimate command to run in CI, and it will delete the exact object you were about to recover.
  • Uncommitted work. Covered above: never committed means never recoverable.

The pattern that avoids all of this

Recovery is the fallback. The durable fix is to stop giving an agent a branch it can delete. Three patterns, in the order I would reach for them:

  1. One worktree per task. Each agent task gets its own checkout and its own branch, so a git branch -D in one task cannot touch another. The git worktrees for AI coding guide covers the layout and the cost of keeping several at once.
  2. A safety branch before the risky step. One command before the agent does anything destructive: git branch backup/agent-$(date +%s). It costs a line of context and it turns “recover from the reflog” into “switch branch”.
  3. Let Git remember the resolution. If the agent’s problem is conflicts rather than deletions, git rerere replays your earlier conflict fixes, and git range-diff shows what an agent changed when it rewrites history. For “which commit introduced the regression”, git bisect is still the fastest route.

And when a patch has to be applied rather than committed, checking the patch before you retry it avoids the class of problem where the same broken patch is applied twice.

Honest take

The reflog is a local safety net with a 30-day expiry on the entries you care about, and it says nothing about a file that was never committed. It is also the difference between “the agent deleted my branch, and I am starting over” and “the agent deleted my branch, and here is the commit”. Use it — but pair it with a worktree per task, because the cheapest recovery is the one you do not need.

Sources

Last updated November 19, 2026. Every command output in this article was reproduced on November 19, 2026 with Git 2.47.3 on Linux in a throwaway repository. The SHAs are from that reproduction and will not match your repository. Verify your own repository state before running any recovery command — nothing here deletes or overwrites work, but git reset --hard does exactly what it says.

Was this article helpful?

Know which coding tool is worth paying for

Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.