Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Cheat sheet

Git commands for working with coding agents

Checkpoint before an agent runs, see what it changed, restore files, stash, run agents in worktrees, bisect and undo a bad commit.

By Checked Beginner
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark
On this page

These git commands keep coding-agent work safe to undo: commit a checkpoint before the agent starts, read the diff before you keep anything, and give each agent its own branch or worktree. If you remember one habit, make it git status and git diff after every agent run. Every command here was run on git 2.53 on 25 November 2026.

The safe loop

git switch -c agent/fix-login     # 1. new branch for this task
git add -A && git commit -m "checkpoint before agent"   # 2. save your starting point
# ... let the agent work ...
git status --short                # 3. which files changed, including new ones
git diff                          # 4. read every changed line
git add -A && git commit -m "agent: fix login redirect"  # 5. keep it, or restore (below)

See what the agent changed

Command Shows
git status --short Changed (M), added (A), deleted (D) and untracked (??) files
git diff Unstaged changes, line by line
git diff --staged Changes already staged for the next commit
git diff --stat Files and line counts only; leaves out untracked files
git diff main...agent/fix-login Everything on the agent’s branch since it left main
git log --oneline -10 The last ten commits, including any the agent made
git show HEAD The latest commit and its diff

Throw away changes you do not want

Goal Command
Undo edits to one file git restore app.py
Undo all edits to tracked files git restore .
Unstage a file but keep the edits git restore --staged app.py
Preview which untracked files would be deleted git clean -n
Delete those untracked files git clean -f (add -d for folders)

git restore and git clean -f cannot be undone. If in doubt, stash first.

Put work aside with stash

Goal Command
Save edits and new files, with a label git stash push -u -m "before agent"
List stashes git stash list
Reapply one and keep it in the list git stash apply stash@{0}
Reapply the latest and drop it git stash pop
Delete one git stash drop stash@{0}

Without -u, stash leaves untracked files behind, and agents create a lot of those.

Run agents in parallel with worktrees

A git worktree is a second working folder on its own branch, sharing one repository. Two agents in two worktrees cannot overwrite each other’s files.

Goal Command
New folder on a new branch from main git worktree add -b agent/task-2 ../repo-task-2 main
List worktrees git worktree list
Remove one when done git worktree remove ../repo-task-2
Clean up records of deleted folders git worktree prune
Delete the branch afterwards git branch -D agent/task-2

Each worktree needs its own dependency install and its own dev server port.

Undo an agent’s commit

Situation Command Effect
Commit is already pushed or shared git revert <sha> New commit that reverses it; history kept
Last commit, not pushed, keep the edits git reset --soft HEAD~1 Commit removed, changes stay staged
Last commit, not pushed, discard everything git reset --hard HEAD~1 Commit and its changes gone
You reset too far git reflog, then git reset --hard <sha> Returns to any recent position of HEAD

Find which commit broke something

git bisect start HEAD <last-good-sha>
git bisect run npm test        # git checks out commits and runs the test for you
git bisect reset               # back to where you started

git bisect run treats exit code 0 as good and 1 to 127 (except 125) as bad, so any test command works.

Common mistakes

Mistake Fix
Letting an agent work with uncommitted changes of your own Commit or stash first, so you can tell its edits from yours
Reading only git diff Run git status too; new files do not appear in a plain diff
git reset --hard on pushed commits Use git revert on anything others may have pulled
Two agents in one folder One branch and one worktree per agent
Letting the agent push or force-push Block it with a hook, as in git guardrails for Claude Code

Check before you keep it

Run the tests, then git diff main...HEAD one last time and read it as you would a colleague’s pull request. To run two agents side by side on the same task, follow parallel refactor with git worktrees. To hand a whole issue to a cloud agent instead, compare background coding agents.

Check your understanding

Git commands for agent work quiz

Checkpoints before an agent run, reading status and diffs, discarding and stashing changes, worktrees for parallel agents, undoing commits safely and bisecting a regression.

8 questions · pass mark 70%

Question 1 of 80 answered
  1. You have uncommitted edits of your own and want to start an agent on the same branch. What should you do first?

Checked against

  • Refactor in parallel with git worktrees and two agents

    Use git worktrees to run Claude Code and Codex in parallel on the same refactor from one written plan, compare tests, diff size and readability, then merge one.

    WorkflowAdvanced1 h 30 min6 steps

  • Git worktree

    An extra working directory attached to the same Git repository, with its own branch checked out, created with `git worktree add`.

    Glossary

  • Git guardrails for Claude Code

    Matt Pocock's skill that sets up a Claude Code hook to block destructive Git commands such as push, reset --hard, clean and branch -D before they run.

    Agent Skill

  • Copilot cloud agent vs Codex cloud vs Jules vs Cursor cloud agents

    Background coding agents compared: Copilot cloud agent, Codex cloud, Jules and Cursor cloud agents on how tasks start, where they run, review and limits.

    Comparison

  • AI-assisted coding

    Working with coding agents: tasks, instruction files, review and verification.

    SkillBeginner

Back to top