This gist is a reusable pattern for repositories where agents often work in multiple git worktrees and where the main branch moves quickly.
It gives you:
- An
AGENTS.mdsnippet that tells Codex how to create and prepare worktrees. - A
worktree-bootstrapscript that creates new worktrees from freshly fetchedorigin/main. - Safe linking of local-only files such as
node_modulesand.envfiles without overwriting real files in the worktree. - Drift reporting against
origin/maininstead of rebasing agent branches.
The important behavior is this:
- New worktrees are created from a fresh remote main.
- Existing worktrees are prepared before dependency installs or tests.
- Local-only shared files are symlinked only when missing.
- Existing branches are not rebased unless the human explicitly asks.
AGENTS.md- reusable global or repo-level instructions.worktree-bootstrap- generic bootstrap helper.install-worktree-bootstrap- optional installer that copies the helper into~/.codex/bin.
Install the helper:
chmod +x install-worktree-bootstrap worktree-bootstrap
./install-worktree-bootstrap myappThen configure the canonical checkout for your repo:
export MYAPP_MAIN_REPO="$HOME/code/work/myapp"Create a new worktree:
myapp-worktree-bootstrap create fix-example "$HOME/.codex/worktrees/fix-example/myapp"Prepare an existing worktree:
myapp-worktree-bootstrap prepare "$HOME/.codex/worktrees/fix-example/myapp"The installed command name is <repo-slug>-worktree-bootstrap, for example
myapp-worktree-bootstrap.
Configuration can be passed either through repo-specific environment variables or through generic ones:
export MYAPP_MAIN_REPO="$HOME/code/work/myapp"
export MYAPP_BASE_REMOTE="origin"
export MYAPP_BASE_BRANCH="main"
export MYAPP_SHARED_LINKS="node_modules .env.development.local"Generic equivalents are also supported:
export WORKTREE_BOOTSTRAP_MAIN_REPO="$HOME/code/work/myapp"
export WORKTREE_BOOTSTRAP_BASE_REMOTE="origin"
export WORKTREE_BOOTSTRAP_BASE_BRANCH="main"
export WORKTREE_BOOTSTRAP_SHARED_LINKS="node_modules .env.development.local"Repo-specific variables win over generic variables.
Copy AGENTS.md into your global Codex instructions or adapt the relevant
section into a repo-local AGENTS.md.
Replace:
/path/to/main/repo/path/to/worktrees/*/repo-namerepo-name-worktree-bootstrapREPO_NAME_MAIN_REPO
with values for your project.
The helper does not overwrite existing files or symlinks. If a destination already exists, it keeps it and reports that it was kept.
The helper does not rebase branches. It fetches the base branch and reports how far the current worktree is ahead or behind.