Today I learned about `git worktree`, which lets you check out multiple branches of the same repo at once. Instead of juggling a single working copy, you can spin up separate directories — one per branch — all linked to the same Git history. This is perfect for running multiple agents in parallel. Give each agent its own worktree on its own branch, let them make changes independently, and then merge their work back together when ready. Here's how it works ``` git worktree add <path-to-folder> -b <branch-name> <starting-point> • <path-to-folder> → where you want the new working directory to live. • -b <branch-name> → create and check out a new branch with this name. • <starting-point> → the commit/branch you’re basing it on (usually main or origin/main). ``` Example: ``` git worktree add feature-2fa -b feature/2fa origin/main git worktree add feature-user-session-tracker -b feature/user-session-tracker ``` Once you're done, `worktree remove` will delete the folder and detach it from the repository's worktree list: ``` git worktree remove feature-2fa ``` ## Re-installing dependencies per worktree Each worktree is its own folder, so your usual dependency folders (like .venv for Python or node_modules for Node) don’t carry over automatically. That means: • Poetry / pyproject.toml: run poetry install inside the worktree. By default Poetry creates a per-project .venv if you set poetry config virtualenvs.in-project true. • pip / requirements.txt: create a venv in the worktree (python -m venv .venv && source .venv/bin/activate) and pip install -r requirements.txt. • npm / package.json: run npm install (or yarn install / pnpm install depending on your setup). Each worktree will have its own environment and node_modules, which are removed automatically when you delete the worktree. ## worktree.sh To make this workflow easier, I wrote a helper script `worktree.sh`. It wraps the common tasks: - `worktree.sh new <branch>`: Creates a new worktree and installs dependencies automatically - `worktree.sh list`: Lists all active worktrees (paths + branches). - `worktree.sh remove <branch>`: Removes the worktree folder and detaches it from git. If run with no arguments, it prints suggested commands for removal. ### Installation You can install it into ~/.local/bin (or somewhere in your $PATH) like this: ``` curl -L https://gist.github.com/jskopek/03b887b98a7d08d18fe36cc0bd084920/raw/worktree.sh -o ~/.local/bin/worktree.sh chmod +x ~/.local/bin/worktree.sh ``` Now you can run `worktree.sh` from anywhere inside your terminal. *Note*: Always review any shell scripts before running them—at least open them up and skim, or even paste them into an LLM for a safety review. Please don’t blindly curl-pipe-execute scripts from the internet.