Give your coding agent its own Git worktree
You are halfway through your own change when a small copy fix comes in. The agent can work on that fix in a second directory while your editor stays open on the original one. This exercise puts a private reminder in your checkout and follows it through the entire handoff.
- Before you start
- Git 2.28 or newer, a POSIX shell and an empty parent directory. Run the blocks in order in the same terminal. The exercise also works without an AI subscription.
- What to verify
- Finish with the new wording on main, your unfinished reminder intact and exactly one checkout listed by Git.
Leave something unfinished
Create a repository with one committed text file. Then leave a reminder as an untracked file. This is a stand-in for local work you want to keep out of the AI patch. The identity settings belong to this disposable repository.
mkdir chair-lab
cd chair-lab
git init -b main
git config user.name "Lab Reviewer"
git config user.email "reviewer@example.invalid"
printf '%s\n' 'Seat status: free' > seat.txt
git add seat.txt
git commit -m "Describe the free seat"
printf '%s\n' 'Call the upholsterer' > notes.localRunning git status --short at this point should show two question marks next to notes.local. That is the only unfinished item in the exercise.
Create a neighbouring checkout
From chair-lab, ask Git to create a sibling directory and a new branch starting at main. The two status commands below are deliberately run from the same terminal. The -C option tells Git which directory to inspect.
git worktree add -b ai/seat ../seat-agent main
git worktree list
git -C ../seat-agent status --short --branch
git status --short --branchThe agent checkout reports ai/seat and a clean tree. Your original checkout reports main and the untracked reminder. Launch Claude Code or Codex in seat-agent if you want to drive the edit with a prompt.
Review one small patch
The first line stands in for the agent changing the wording. If your agent already made that same edit, begin with the diff. Inspect the change before staging it and then compare the committed branch against main.
printf '%s\n' 'Seat status: available' > ../seat-agent/seat.txt
git -C ../seat-agent diff -- seat.txt
git -C ../seat-agent add seat.txt
git -C ../seat-agent commit -m "Call the seat available"
git diff main...ai/seat -- seat.txtThe diff should replace free with available on a single line in seat.txt. If your agent also revised the seating manifesto, ask for another pass before continuing.
Accept the wording and fold up the spare chair
Merge from chair-lab. A fast-forward is possible here because main still points at the original commit. Read both files before retiring the second checkout and its merged branch.
git merge --ff-only ai/seat
cat seat.txt notes.local
git worktree remove ../seat-agent
git branch -d ai/seat
git worktree listExpect one line describing the available seat, one reminder about the upholsterer, and a final worktree listing with only chair-lab on main.
Try this prompt with your agent
In this checkout, change “Seat status: free” to “Seat status: available” in seat.txt. Show the diff and stop before staging. Tell me which branch you are on and which tracked files changed.A trap worth catching
A worktree separates working files and the staging area. Both checkouts still share branch history and run under your account. If git worktree remove reports local edits, inspect them in that checkout before deciding what to keep.