
You are halfway through a catalog redesign when a bug lands: the library app shows “available” for a book that has no copies left. You could stash your changes and switch branches, but the redesign is already running locally and you want to keep it open. A Git worktree lets you put the fix on a separate branch in a separate directory while leaving your current checkout alone.
That solves the checkout problem. It does not solve the database problem. If the two copies still point to the same local database, the agent can change the data your unfinished work depends on without ever editing its files.
Start the fix from the branch that should receive it
In this fictional repository, the redesign is checked out in library, and the availability fix belongs on the current main branch. Assuming origin is the expected remote, update its tracking refs and create a sibling checkout:
git status --short --branch
git fetch origin
git worktree add -b fix/zero-copy-label ../library-zero-copy origin/main
git worktree list
git -C ../library-zero-copy status --short --branch
Fetching updates local knowledge of the remote without merging into the current branch. The worktree command creates a new branch from the explicit base, so the redesign’s uncommitted files are not part of the fix. If your team patches a release branch instead, use that branch or a known commit. Do not omit the base and assume Git will pick the branch you intended.
Use a new branch name. Git normally refuses to check out a branch already in use by another worktree. Treat that refusal as a cue to check git worktree list, not as a reason to override the safeguard.
Know what the second directory actually separates
Each worktree has its own checked-out files, HEAD and index. In practice, the files you edit or stage for the fix do not change what is staged for the redesign. But both checkouts are linked to one repository: they share the object database, normal branch and tag refs, and repository configuration by default. You can inspect the fix commit from the original checkout without switching branches. Conversely, changing a shared setting or moving a branch ref can affect both.
A worktree is not a sandbox. Open the new folder as the task’s working directory and use your normal filesystem and execution controls if the agent should not write outside it. A different current directory alone does not prevent a process from reaching sibling folders or shared services.
Give the bug fix its own local data
Build the fix’s local configuration from the project’s example configuration, not by copying a live credentials file. Give it a different application port and a disposable database populated with fictional books. Use a separate cache instance or a dedicated, verified namespace; a namespace is not enough if a command flushes the entire cache. Separate queue names, workers and writable output paths if the app uses them. Check the settings the running process actually picked up before doing anything that writes data.
With Docker Compose, a distinct project name helps keep project-scoped resources apart. It does not change explicitly published host ports or stop two services from mounting the same host path. An external volume also remains external: Compose uses the existing volume rather than creating a private copy just because the project name changed.
Keep the fix when you remove the checkout
Before handing back the patch, check the branch and diff in the fix folder, then return to the original folder and verify that the redesign’s changes are still there. Keep the fix’s commits on its branch and stop only the processes you started for that worktree. Check for local files you still need, including ignored configuration and artifacts, before removing the folder.
Once the checkout is no longer needed, use git worktree remove ../library-zero-copy from the original checkout. If Git refuses, inspect what is still there rather than forcing removal. Removing the linked checkout does not delete its branch. The fix and the redesign can now move forward independently without either having to wait for the other.