|
| 1 | +# Git Worktrees |
| 2 | + |
| 3 | +All repositories are set up as **bare clones with worktrees**. This enables parallel work — multiple agents (or a human and an agent) can work on different issues in the same repository simultaneously without conflicts, stashing, or context-switching. |
| 4 | + |
| 5 | +## Why worktrees |
| 6 | + |
| 7 | +| Problem with traditional clones | How worktrees solve it | |
| 8 | +| -------------------------------------------- | --------------------------------------------------------- | |
| 9 | +| Only one branch checked out at a time | Each issue gets its own worktree — parallel by default | |
| 10 | +| Switching branches requires clean state | Worktrees are independent — no stashing or committing WIP | |
| 11 | +| Agent work blocks human work on same repo | Different worktrees, no interference | |
| 12 | +| Default branch gets dirty during development | `main/` worktree is always a clean reference | |
| 13 | + |
| 14 | +## Repository layout |
| 15 | + |
| 16 | +```text |
| 17 | +<repo-root>/ |
| 18 | +├── .bare/ # bare git data (the actual repository) |
| 19 | +├── .git # file containing: gitdir: ./.bare |
| 20 | +├── main/ # worktree: default branch (always clean, never worked in directly) |
| 21 | +├── 42-add-pagination/ # worktree: issue #42 in progress |
| 22 | +└── 99-fix-null-ref/ # worktree: issue #99 in progress |
| 23 | +``` |
| 24 | + |
| 25 | +- **`.bare/`** — the shared git object store. All worktrees share this. |
| 26 | +- **`.git`** — a file (not a directory) that points git tooling to `.bare/`. |
| 27 | +- **`main/`** — the default branch worktree. Kept as a clean reference. Used for diffing, reading docs, running comparisons. Never directly committed to. |
| 28 | +- **`<N>-<slug>/`** — one worktree per issue in flight. Named by issue number and a short slug. Branch name matches the folder name. |
| 29 | + |
| 30 | +## Remotes |
| 31 | + |
| 32 | +Every repository has exactly two remotes (or one, if it is not a fork): |
| 33 | + |
| 34 | +| Remote | Points to | Required | Purpose | |
| 35 | +| -------------- | ---------------------------- | -------- | ------------------------------------------------ | |
| 36 | +| **`origin`** | Our copy on the server | Always | Push branches, open PRs, CI runs against this. | |
| 37 | +| **`upstream`** | The parent repo (forks only) | Forks | Track upstream changes, sync the default branch. | |
| 38 | + |
| 39 | +No other remotes are added. This keeps the model simple and predictable for both humans and agents. |
| 40 | + |
| 41 | +### How it works in practice |
| 42 | + |
| 43 | +- **Non-fork repos** — only `origin` exists. Branches are pushed to `origin`, PRs are opened against `origin`. |
| 44 | +- **Forked repos** — `origin` is our fork, `upstream` is the original repository. The default branch tracks `upstream` for syncing; feature branches are pushed to `origin` and PRs are opened from `origin` into `upstream`. |
| 45 | + |
| 46 | +### Fetch configuration |
| 47 | + |
| 48 | +Both remotes are configured with full refspecs so `git fetch --all --prune` keeps everything current: |
| 49 | + |
| 50 | +```text |
| 51 | +[remote "origin"] |
| 52 | + fetch = +refs/heads/*:refs/remotes/origin/* |
| 53 | +
|
| 54 | +[remote "upstream"] # forks only |
| 55 | + fetch = +refs/heads/*:refs/remotes/upstream/* |
| 56 | +``` |
| 57 | + |
| 58 | +## Setup (one-time per repository) |
| 59 | + |
| 60 | +```powershell |
| 61 | +# Clone as bare into .bare/ |
| 62 | +git clone --bare https://github.com/<owner>/<repo>.git .bare |
| 63 | +
|
| 64 | +# Create the .git pointer file |
| 65 | +Set-Content .git "gitdir: ./.bare" -NoNewline |
| 66 | +
|
| 67 | +# Configure fetch refspec (bare clones don't set this automatically) |
| 68 | +git -C .bare config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' |
| 69 | +
|
| 70 | +# Fetch remote branches |
| 71 | +git -C .bare fetch origin |
| 72 | +
|
| 73 | +# Create the default branch worktree |
| 74 | +git -C .bare worktree add ../main main |
| 75 | +``` |
| 76 | + |
| 77 | +> The [Checkout-GitHubRepo](https://github.com/MariusStorhaug/.dev/blob/main/.github/Checkout-GitHubRepo.ps1) script automates this for all repositories. |
| 78 | +
|
| 79 | +## Working on an issue |
| 80 | + |
| 81 | +```powershell |
| 82 | +# From the repo root (where .bare/ lives) |
| 83 | +git -C .bare worktree add ../42-add-pagination -b 42-add-pagination main |
| 84 | +
|
| 85 | +# Open in VS Code |
| 86 | +code 42-add-pagination |
| 87 | +``` |
| 88 | + |
| 89 | +Then follow the normal Implement flow: initial commit → push → draft PR → build → finalize. |
| 90 | + |
| 91 | +## Cleanup after merge |
| 92 | + |
| 93 | +```powershell |
| 94 | +# Remove the worktree |
| 95 | +git -C .bare worktree remove 42-add-pagination |
| 96 | +
|
| 97 | +# Prune if needed (removes stale worktree references) |
| 98 | +git -C .bare worktree prune |
| 99 | +``` |
| 100 | + |
| 101 | +The sync script handles this automatically — worktrees whose branch no longer exists on any remote are removed during the next sync. |
| 102 | + |
| 103 | +## Parallel agents |
| 104 | + |
| 105 | +The worktree model enables multiple agents to work in the same repository at the same time: |
| 106 | + |
| 107 | +- Each agent operates in its own worktree (its own issue folder). |
| 108 | +- No merge conflicts during development — conflicts only surface at PR merge time. |
| 109 | +- The default branch worktree provides a stable reference for all agents to read from. |
| 110 | +- CI runs independently per PR, so agents don't block each other. |
| 111 | + |
| 112 | +```text |
| 113 | +Agent A: working in 42-add-pagination/ |
| 114 | +Agent B: working in 43-fix-auth-flow/ |
| 115 | +Human: reviewing in main/ or reading docs |
| 116 | +``` |
| 117 | + |
| 118 | +## VS Code integration |
| 119 | + |
| 120 | +- **Open the worktree folder directly** — VS Code's Git extension auto-detects the git context. |
| 121 | +- **Multi-root workspace** — add multiple worktrees to one window (`File → Add Folder to Workspace`) to reference `main/` while coding in another. |
| 122 | +- **Terminal cwd** — ensure the integrated terminal is in the worktree folder, not the bare root. |
| 123 | + |
| 124 | +## Rules |
| 125 | + |
| 126 | +1. **Never commit directly to the default branch worktree.** It exists for reference only. |
| 127 | +2. **One worktree per issue.** The folder name matches the branch name: `<N>-<slug>`. |
| 128 | +3. **Clean up after merge.** Remove the worktree and let the sync script prune stale branches. |
| 129 | +4. **All git config lives in `.bare/`** — it applies to every worktree automatically. |
| 130 | +5. **Only `origin` and `upstream` remotes.** No other remotes. `upstream` only exists for forks. |
0 commit comments