Topic 3 of 5 · 4 min read
Checkpoints, commits and resuming after a crash
Goal: Choose checkpoint points for a long task so that a crash or a bad change loses at most one step and can be rolled back.
Commit after every verified step, so that a crash or a bad change costs one step and the way back is a single command. A long job will fail somewhere. The only choice is how much it loses.
Check yourself · 1 of 3
When should an agent commit during a long task?
Why it matters
An agent may run for hours across hundreds of edits. The process can be killed, the machine can reboot, the API can time out, or the agent can take a wrong turn and spend 40 minutes building on it. Without checkpoints you restore by hand. With them you step back to the last good point.
How to do it
- Define a step. One feature, one bug fix, one refactor move. Small enough that the diff fits on a screen and one test command checks it.
- Commit when the check passes. Not before. Use a message that names the step id, so the history reads as a log. Never commit work that fails its own test, except as an explicit
wip:commit on a throwaway branch. - Work on a branch or a worktree.
git worktree add ../job-123 -b agent/job-123gives the agent its own directory. Your main checkout stays untouched. - Know the editor's own checkpoints and their limits. Claude Code snapshots file edits made through its tools and can rewind them. It does not track changes made by shell commands, so files changed by
sedor a build script are not covered. Git still is. - Rewind with the right tool. An unshared bad commit:
git reset --hard <good>on the agent's branch. A commit already pushed:git revert <sha>, which adds a new commit instead of rewriting history. To find which step broke a test:git bisect run npm test. - Resume, don't restart. After a crash, a new session reads the progress file, runs
git statusandgit log -3, and decides: finish the uncommitted edit, orgit stashit and redo the step.
Before and after
Weak: the agent edits for three hours and commits once at the end with git add -A && git commit -m "work". One bad change in hour one is now tangled with everything after it.
Better:
npm test -- features/F-014 \
&& git add src/features/F-014 tests/F-014 \
&& git commit -m "F-014: export invoices to CSV"
The commit only happens if the test passes, and it adds named paths, not everything.
What goes wrong
- One giant commit. You cannot revert one idea without losing the others.
- Committing red. A broken commit becomes the "good" base the next session trusts.
git add -Aover secrets and build output. Stage named paths.- Rewriting shared history.
reset --hardand force-push on a pushed branch destroy other people's work. Userevertthere.
Examples
Resuming after a crash
git status --short
git log --oneline -3
cat PROGRESS.md | head -20
The status shows M src/export.ts uncommitted. The last commit is F-013. The note says F-014 was in progress. The new session runs the F-014 test, sees it fails half way, runs git stash, and redoes the step from a clean tree.
Finding the step that broke the build
Test tests/totals.test.ts fails on commit 40 and passed on commit 12. The agent made one commit per feature, so:
git bisect start HEAD <commit-12>
git bisect run npm test -- tests/totals.test.ts
In about five runs it names the exact feature commit that introduced the bug.
Key points
- Commit after each verified step so a crash or bad change loses at most one step.
- Commit only when the step's own check passes, and stage named paths rather than everything.
- Use a separate branch or git worktree so the agent's work never touches your main checkout.
- Editor checkpoints do not cover changes made by shell commands, so git remains the safety net.
- Use reset on unshared work, revert on pushed commits, and bisect to find which step broke a test.
Common mistakes
- Committing once at the end of a long run.
- Committing code whose test is failing, so the next session trusts a broken base.
- Using git reset --hard or a force-push on a branch other people have pulled.
- Assuming editor rewind restores files changed by shell commands.
Check yourself · 2 of 3
A bad commit is already pushed to a shared branch. How do you undo it safely?
Check yourself · 3 of 3
Claude Code's checkpoints rewind file edits made through its edit tools. A build script the agent ran also rewrote 30 files. What happens on rewind?
Practice items, not counted towards your mastery.
Start here
- Effective harnesses for long-running agents (Anthropic) (opens in a new tab)ArticleFree
Describes the initializer session, feature list, progress file and one-feature-per-session pattern this skill teaches.
- Effective context engineering for AI agents (Anthropic) (opens in a new tab)ArticleFree
Covers compaction, structured note-taking and sub-agent hand-offs for tasks that outgrow the context window.
- Checkpointing (Claude Code docs) (opens in a new tab)DocsFree
Explains how Claude Code snapshots edits, rewinds code and conversation, and what checkpoints do not cover.
More for this topic
- git-revert (Git documentation) (opens in a new tab)DocsFree
The safe way to undo a commit that is already shared, by adding a new commit.
- git-bisect documentation (opens in a new tab)DocsFree
Finds the commit that broke a test by binary search, which small agent commits make cheap.