Resume a Coding Agent Session in the Right Git Worktree
Prepared and checked using AI. Sources are linked in the article.
To resume a coding agent session in a Git worktree, locate the worktree first, check its branch and pending changes, then resume the specific conversation. Before asking the agent to edit, confirm its active directory again. A saved conversation and a Git checkout are separate things: each worktree has its own HEAD and index, while agents have their own rules for finding and reopening sessions.
Find the worktree before opening the agent
From any checkout of the repository, run git worktree list. It shows each registered path, checked-out commit, and branch. For example, suppose a task called auth-fix belongs in /src/app-auth, while /src/app is the main checkout:
$ git worktree list
/src/app a1b2c3d [main]
/src/app-auth e4f5a6b [auth-fix]
Treat the path and branch as a pair. A remembered session name alone does not tell you where its next file edit will land. If the expected branch appears under another path, enter that path; do not try to check out the same branch in the main checkout. If no path matches, investigate before creating or resuming anything.
Once inside the candidate directory, print the working-tree root with git rev-parse --show-toplevel. This matters when a terminal opens in a nested package directory: the root tells you which checkout contains the files the agent will change. Then use git status --short --branch to see the branch and changed paths.
cd /src/app-auth
git rev-parse --show-toplevel
git status --short --branch
The root should be /src/app-auth, and the status header should name auth-fix. Read the rest of the status output before resuming. Existing edits may be the unfinished work you want to preserve, changes made by another process, or evidence that you opened the wrong checkout. A clean status does not prove the session is wrong: earlier work may already have been committed.
If you need to identify pending work, compare unstaged changes with git diff and staged changes with git diff --cached. git diff --stat and git diff --cached --stat give shorter file summaries; inspect the full diff when the summary cannot establish which task the changes belong to. For more on the directory and branch relationship, see Git Worktrees: Why AI Agents Need Them.
Match the session to the tool
After checking the checkout, choose a session by its name or ID when possible. A “latest” command is convenient only when you already know which conversation was most recent in that tool’s relevant scope. The commands below reopen conversation history; they do not replace the Git checks.
| Agent | Resume a chosen conversation | Resume the latest conversation |
|---|---|---|
| Claude Code | claude --resume <name-or-id> | claude --continue |
| Codex CLI | codex resume <session-id> | codex resume --last |
| Gemini CLI | gemini --resume <session-id> | gemini --resume |
Claude Code
Claude Code’s --continue opens the most recent conversation in the current directory, while --resume opens a picker or accepts a name or ID. If several tasks have used the checkout, select the intended conversation instead of assuming the latest one is correct. A descriptive session name helps distinguish a bug fix from a refactor, but the name is only an identifier for the conversation.
There is an extra directory check for Claude sessions that used worktrees. Claude Code can return a resumed worktree session to its worktree; if that worktree was deleted, it resumes in the launch directory. Thus, even launching Claude from the path you selected is not the final proof of where the resumed agent is working. Before the first edit, ask it to run git rev-parse --show-toplevel and git status --short --branch, and compare the output with your earlier check.
Claude’s session picker can also show conversations from other worktrees of the repository. If you choose one, pay attention to the directory it enters. If you intended to continue work in a different checkout, start a new conversation there and provide the needed context; do not rely on a session title to redirect the old conversation’s files.
Codex CLI
Codex supports codex resume <session-id> and scopes codex resume --last to the current working directory unless --all is passed. Use an ID when you have several conversations associated with a task, or use the picker by running codex resume without an ID. Avoid --all for a routine continuation: it widens selection beyond the current directory.
Codex may ask which directory to use when the current directory differs from the saved session directory. Read that prompt against the worktree path you verified. If you deliberately need to set the agent’s directory, the global -C or --cd flag provides an explicit path, for example codex -C /src/app-auth resume <session-id>. That path choice still deserves a fresh Git check after the session opens, particularly if the saved conversation discussed another branch.
Gemini CLI
Gemini CLI stores sessions by project directory and offers gemini --list-sessions, gemini --resume <id>, and gemini --resume for the latest session. Run the list command from the intended worktree. If the conversation you expect is absent, confirm the worktree path before assuming the history has disappeared; switching project directories switches which sessions are listed.
cd /src/app-auth
gemini --list-sessions
gemini --resume <session-id>
The interactive /resume browser is useful when the ID is unknown. Choose by the conversation preview and task, then have the agent confirm the Git root and branch before it writes. A session from the main checkout may contain useful discussion, but reopening it does not make the main checkout’s files and the worktree’s files interchangeable.
Make the first resumed turn a location check
The first prompt after resuming should establish current state rather than immediately saying “continue.” One practical prompt is:
Before editing, show the working-tree root, current branch, and Git status.
Summarize the unfinished task from this conversation. Wait for my next
instruction if the root is not /src/app-auth or the branch is not auth-fix.
Compare the agent’s output with the commands you ran in your own terminal. This catches a session that reopened in an unexpected checkout and a terminal tab that quietly remained in the main checkout. It also makes the agent restate what it believes the task is before acting on old context. If that task summary conflicts with the current diff, inspect the conversation and files before asking it to continue.
For long-running work, keep a small handoff in the task or commit history: the intended worktree path, branch, session identifier, completed changes, and next action. The identifier gets you back to the conversation; the Git checks establish where new changes will go. If you run multiple agents, keep their tasks and checkouts distinct so a session name is not your only way to tell work apart.
Handle a missing or moved worktree
If git worktree list no longer shows the expected path, do not resume and start editing from whichever checkout happens to be open. Check whether the worktree was removed, its directory was moved, or the task’s changes were committed elsewhere. A session transcript can remain available after its original checkout is gone, but its former working files may not be there.
If a worktree was moved outside Git, run git worktree repair inside the moved worktree after identifying its new directory. From another checkout, pass the new path instead, for example git worktree repair /src/app-auth. If the directory was manually deleted, check git worktree list for a registration pointing to the missing path: stale registration can block git worktree add. Run git worktree prune --dry-run --expire now to see which missing worktrees Git would unregister. If the output contains only registrations you intend to discard, run git worktree prune --expire now, then check git worktree list again. If the branch still exists and is not checked out elsewhere, git worktree add <path> <branch> can then create a checkout for it. Inspect the branch and its changes before resuming the old session: recreating a checkout does not restore uncommitted files from a deleted directory.
If the expected worktree exists but its status shows unrelated edits, stop before changing branches or removing files. Identify who or what owns those edits, then open the proper checkout or create a separate task branch. Once the task is complete, review the resulting diff in its own worktree before merging work from parallel agents.
Where Parallel Code fits
Parallel Code is our free, open-source desktop app for macOS and Linux. It runs agents including Claude Code, Codex CLI, and Gemini CLI in parallel, each isolated in its own Git worktree, and provides a diff-first review surface. When returning to parallel tasks, use each task’s worktree and diff as the place to check what remains before continuing the agent.
Frequently asked questions
Can I resume a session from the main checkout and still edit the worktree?
That depends on the agent and the saved session. Claude Code can re-enter a worktree used by its session, while Codex may ask which directory to use when saved and current directories differ. Check the active Git root and branch after resuming rather than relying on the directory from which you launched the command.
Does resuming a conversation restore uncommitted Git changes?
Resuming reopens conversation context; it is not a substitute for the worktree’s files and index. Inspect git status in the intended checkout. If the worktree was deleted, recreate or recover the files separately before asking the agent to continue.
Should I use the latest-session shortcut?
Use it when you have confirmed both the worktree and which conversation was latest there. If several sessions could match, list or pick a session by its task, name, or ID. Confirm the directory once more after opening it.
What if the branch name is right but the directory is wrong?
Find the path registered for that branch with git worktree list and enter it. A branch label in a prompt or terminal tab does not identify the checkout containing the files you mean to edit. Compare the Git root, status, and pending diff before continuing.