How to use git worktrees for AI coding agents
A git worktree is a second working folder for the same repository, checked out on its own branch. For AI coding agents, a worktree per agent means each one edits its own copy of the files, so several can work in one repository at once without their changes mixing. Moontors, an agentic development environment (ADE), can make, use and clean up a worktree for each session.
By hand
Section titled “By hand”# a branch and a folder for one agent's task, beside your checkoutgit worktree add -b fix-login ../myapp-fix-logincd ../myapp-fix-login# start the agent here, then review and merge the branch as usualgit worktree remove ../myapp-fix-loginTwo things to remember: files git doesn’t track, such as .env, aren’t in the new worktree, and a
worktree you forget to remove stays on disk with its branch.
With Moontors
Section titled “With Moontors”When the folder you pick in Create session is in a git repository, turn on Run in a new worktree. Moontors then:
- makes the worktree from your checkout’s current commit, with no branch unless you type one in the
Branch field, such as
al/fix-the-login, which makes a branch of exactly that name; - puts the worktree under
~/.moontors/worktrees/, outside your own folders; - copies in the untracked files your project lists, such as
.env; - starts the session there.
The card shows the repository and the branch, or the commit for a worktree with no branch, and the session’s Git view opens on its worktree, with its changes beside it. When you’re done, archiving or deleting the session asks, in one dialog, what goes with the worktree: its uncommitted changes, the worktree itself, its branch and the branch on its remote, each a box saying what it loses, with nothing that loses work ticked for you. Nothing stops until you confirm, so Cancel leaves the agent and its terminals running. While a fork or another session still works in the worktree, it stays for them. With no branch, it offers to make one there to keep the commits no branch holds. See Worktrees for the details.
Things to know
Section titled “Things to know”- A worktree starts from the last commit, so your checkout’s uncommitted changes aren’t in it, unless you start it with New worktree with my changes, which copies them in.
- Each worktree is a full copy of the files, so dependencies installed in your checkout aren’t in it: the agent may need to install them first.
- A fork of a session shares its worktree, as does a session you start in the worktree’s folder: they edit the same files, and share its terminals and its Git view.
- You merge each agent’s work yourself, as you would a teammate’s: name a branch for it, or make one later from the Git view.
Questions
Section titled “Questions”What is a git worktree?
Section titled “What is a git worktree?”A second working folder for the same repository, on a branch of its own or on a commit, as git calls detached. Agents in separate worktrees each edit their own copy of every file.
Are my uncommitted changes in a new worktree?
Section titled “Are my uncommitted changes in a new worktree?”Only if you ask for them. A worktree starts from the last commit, so your uncommitted changes and
the files git doesn’t track stay in your checkout, but for those your project lists for Moontors to
copy, such as .env. New worktree with my changes, on a project’s create menu, copies your
uncommitted changes in, new files included, never those git ignores (see
Worktrees).
What happens to the worktree when I’m done?
Section titled “What happens to the worktree when I’m done?”Archiving the session removes it, or, while another session works in it too, archiving the last of them does. Moontors asks first, before anything stops, if it holds uncommitted changes, or commits that aren’t pushed or, with no branch, that no branch holds, which it offers to keep on a branch it makes there; a branch stays whenever it holds work.