
How to Use Git Effectively with Coding Agents
Agentic AISetup time: 15 minutes to learn; ongoing workflow change Difficulty: Intermediate Target use: Developers using Claude Code, Cursor, or similar coding agents in a Git-versioned project
Summary
- Treat commits as reversible checkpoints — agents can rewrite, combine, and undo commit history
- Use branches for every agent-led experiment before merging to main
- Open each agent session with “Review changes made today” for automatic context loading
- The commit history is not fixed — treat it as a curated narrative, not an immutable log
- Key recovery prompt for any git mess: “Sort out this git mess for me”
Purpose
Agents change Git workflows in ways that aren’t obvious until something breaks. This stack documents the core patterns from Simon Willison’s agentic engineering guide for getting Git and coding agents to work together without creating an unmaintainable commit history or losing work.
Prerequisites
- Git repository initialized
- Coding agent (Claude Code, Cursor, Copilot, etc.) configured
- Familiarity with basic git commit/branch/merge commands
Steps
1. Start every session with a history summary
Open a new agent session with: “Review changes made today.” The agent runs git log and loads recent commits and messages as context. This prevents the agent from re-implementing what was already done in prior sessions and gives it an accurate starting state without manually pasting diffs.
2. Commit frequently as reversible checkpoints
Ask the agent to commit after each logical unit of work — not just at the end of a session. Small commits are easier to undo. If a direction turns out wrong, a simple “Undo last commit” restores the prior state without a manual git reset HEAD~1.
3. Let agents rewrite commit messages Agent-generated commits are often poorly labeled. Use prompts like “Combine the last three commits with a better commit message” to consolidate and describe what actually happened. This is editorial work — treat the history as documentation.
4. Branch before every experimental change Before asking an agent to try a significant refactor, architecture change, or anything described as “let’s try”: create a branch. Use “Create a branch for this experiment before you start.” Merge back to main only after review. Branches make it safe to let the agent go further than you might manually, because recovery is clean.
5. Sync branches with a conversation When branches diverge, don’t immediately rebase or merge manually. Ask the agent: “Discuss options for integrating changes from main.” Agents can reason through conflicting change sets and propose a merge strategy before executing. This catches issues that silent rebasing misses.
6. Use “sort out this git mess” as a universal recovery prompt Staging conflicts, detached HEAD states, messy merge artifacts — rather than manually resolving, use: “Sort out this git mess for me.” The agent will assess the state, propose a resolution, and execute it. For anything involving data loss risk, ask it to explain before acting.
7. Find lost work via reflog prompts If code disappears — from an undo, a reset, or a merge — use: “Find and recover my code that does [description].” The agent searches reflog and stashed branches for the lost work. This recovers most common loss scenarios that would otherwise require manual reflog archaeology.
8. Extract modules with preserved history
When extracting a library or module into a new repository, the agent can run git filter-repo or equivalent to migrate code with commit history, author names, and dates intact. Prompt: “Extract [module] into a new repository while preserving its commit history.”
The Trap
Treating commit history as immutable. Agents make many small changes quickly, and accepting every auto-generated commit message creates a log that is noise, not documentation. The inverse is also true: if you let an agent work for a full session without committing, recovery from any mistake requires a reset to the last clean state. Commit small and often, then curate the history at the end of the session. The commit history is not a log of what happened — it is documentation of what matters.
Cited References
- Using Git with Coding Agents — Simon Willison’s Agentic Engineering Patterns
- Agentic Engineering Patterns — Simon Willison
- git filter-repo documentation
Content created with AI assistance and reviewed for accuracy.
Join the conversation
Stack Insiders is our free community for readers who want to go deeper — share resources, ask questions, and connect with others across every vertical we cover.
Join Stack Insiders →