Why coding agents should work on a copy of your repo
A coding agent should work on its own copy of your repository, on its own branch, so that nothing it does touches your working files until you have read the diff and merged it yourself. Git worktrees are the simplest way to do this: one repository, several separate working directories, each on a different branch. This guide explains what goes wrong without isolation, how worktrees work, the…
When using a coding agent, it is crucial to work on a copy of your repository on its own branch. This ensures that the agent's modifications do not affect your working files until you have reviewed the changes and merged them yourself. Git worktrees provide an easy solution for this purpose, allowing for multiple separate working directories within a single repository, each on a different branch.
Working on a copy of your repository with a separate branch offers several benefits. Mixed changes are avoided, as the agent's edits and your half-finished work remain in distinct sets. The agent changes a file independently of your editing, preventing disruptions in your development environment. Reviewing the agent's work becomes more manageable, as there is a clear before-and-after diff, making it easier to understand the changes made.
Undoing undesirable results is also simpler since discarding the agent's changes won't impact your own work.
Git worktrees function by creating additional working directories attached to the same repository. Each worktree has its own branch, files on disk, and staging area, while sharing the underlying commit history. This sharing allows for real-time visibility and comparison of changes between worktrees without the need for additional cloning or pushing/pulling operations.
To create a worktree and branch for an agent's task, use the command `git worktree add ../myproject-fix-login -b agent/fix-login`. To view the existing worktrees, run `git worktree list`. Review the changes from the main directory when the agent has finished working, using `git diff main...agent/fix-login`. If satisfied, merge the agent's changes with `git merge agent/fix-login` and remove the worktree directory with `git worktree remove ../myproject-fix-login`. Additionally, use `git branch -d agent/fix-login` to delete the branch once it is no longer needed.
However, be aware of potential pitfalls. Files not tracked by Git are not copied to the new worktree, so you may need to run an install step and provide any local configuration the project requires. Avoid copying secrets into an agent-accessible directory and ensure you have the necessary permissions and sandboxes in place for safety.
Create and remove worktrees promptly to optimize disk space and manage shared machine resources effectively. Keep the main branch up-to-date with `git pull` to prevent stale branches, and test the workflow manually before relying on it.
While worktrees provide isolation from your working files, they do not restrict the agent's overall capabilities on your computer. Agents can still access other folders, use the network, and interact with anything your user account can access. For enhanced protection, consider limiting tools and permissions, implementing approval processes for risky commands, and using containers or virtual machines for stronger separation.
By using worktrees for organization and review, combined with appropriate permissions and sandboxes, you can effectively manage the workflow involving coding agents and maintain control over your machine resources.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.