Git Worktree Workflows
Master Git worktrees using the gw CLI tool for optimized parallel development workflows.
MANDATORY: Always Use gw, Never Raw git worktree
Non-negotiable in any repo where gw is installed. Check with which gw; if missing, suggest brew install mthines/gw-tools/gw or npm install -g @gw-tools/gw.
| Instead of |
Use |
Why it matters |
git worktree add ../feat feat |
gw checkout feat |
Auto-copies files, runs hooks, remote probe, auto-cd |
git worktree remove feat |
gw remove feat |
Prunes orphan branch, protects main |
git checkout -b feat |
gw checkout feat |
Isolated worktree instead of in-place branch switch |
git checkout main (when main is another wt) |
gw checkout main |
Detects existing worktree and navigates instead of erroring |
cd ../feat |
gw cd feat |
Partial matching, works from any depth |
git fetch && git checkout pr-branch |
gw pr <number> |
Handles fork PRs, auto-copies, runs hooks |
Manually cp ../main/.env .env after worktree |
Configure autoCopyFiles in gw init |
One-time setup, auto-copies forever after |
Rules
| Rule |
Description |
| fundamentals |
HIGH - Core concepts of Git worktrees, what they share/don't share, when to use them |
| creation |
HIGH - Creating worktrees with gw checkout, remote fetch behavior, auto-copy files |
| navigation |
MEDIUM - Navigating with gw cd and gw checkout, shell integration setup |
| inspection |
LOW - Listing worktrees with gw list, understanding worktree states |
| cleanup |
MEDIUM - Removing worktrees, gw clean, gw prune, disk space management |
| troubleshooting |
HIGH - Common errors and solutions, recovery procedures |
Workflow Patterns
| Pattern |
Description |
| feature-branch |
HIGH - Feature development workflow with worktrees |
| hotfix |
HIGH - Urgent bug fixes without interrupting feature work |
| code-review |
HIGH - Review PRs in isolated environments with gw pr |
| parallel-testing |
MEDIUM - Test across Node versions or configurations |
Quick Reference
Primary Commands
| Task |
Command |
| Create worktree (new branch) |
gw checkout feat/name |
| Create worktree (alias) |
gw co feat/name or gw add feat/name |
| Create from different branch |
gw checkout feat/name --from develop |
| Create from staged files (extract WIP) |
gw checkout feat/name --from-staged |
| Create without auto-navigation |
gw checkout feat/name --no-cd |
| Skip remote probe (offline mode) |
gw checkout feat/name --no-fetch |
| Skip pre/post-checkout hooks |
gw checkout feat/name --no-hooks |
| Navigate to worktree by name |
gw cd feat/name |
| Navigate by partial match |
gw cd feat (matches first worktree with "feat") |
| Navigate to branch (even if in other wt) |
gw checkout main |
| List all worktrees |
gw list |
| Check out a PR into a worktree |
gw pr 123 |
| Check out a PR by URL |
gw pr https://github.com/user/repo/pull/123 |
| Update with main (merge or rebase) |
gw update |
| Update from specific branch |
gw update --from develop |
| Sync auto-copy files to current worktree |
gw sync |
| Sync specific files |
gw sync .env .env.local |
| Remove worktree + delete local branch |
gw remove feat/name |
| Remove worktree but keep branch |
gw remove feat/name --preserve-branch |
| Remove multiple worktrees by name |
gw remove feat-a feat-b feat-c |
| Remove every worktree under a scope |
gw remove feat/* (with shell integration) or gw remove 'feat/*' |
| Remove every worktree starting with name |
gw remove fix* (greedy across /) |
Preview a destructive gw remove |
gw remove --dry-run feat/* |
| Batch remove safe (committed, pushed) wts |
gw clean |
| Preview batch cleanup |
gw clean --dry-run |
| Full cleanup: worktrees + orphan branches |
gw prune |
| Get repo root path (worktree-aware) |
gw root |
| Protect branch from cleanup |
gw protect [branch] (default: cwd branch) |
| Remove protection from branch |
gw unprotect [branch] (default: cwd branch) |
Setup Commands
| Task |
Command |
| Clone repo and configure gw (interactive) |
gw init git@github.com:user/repo.git --interactive |
| Init gw in existing repo |
gw init --auto-copy-files .env --post-checkout "pnpm install" |
| Show current gw config |
gw show-init |
Install shell integration (required for cd) |
eval "$(gw install-shell)" (add to ~/.zshrc) |
| Remove shell integration |
gw install-shell --remove |
Proxy Commands (pass-through to git worktree)
| Task |
Command |
| Lock worktree |
gw lock feat/name |
| Unlock worktree |
gw unlock feat/name |
| Repair worktree |
gw repair |
| Move worktree |
gw move feat/name new-path |
| Prune stale metadata |
gw prune --stale-only |
Key Principles
gw checkout is canonical. gw add and gw co are aliases — prefer gw checkout in explanations.
- One branch per worktree. Same branch cannot be checked out in two worktrees.
gw checkout <existing> detects this and navigates instead.
- Remote probe before treating a branch as new.
gw checkout runs git ls-remote (3s timeout) so a teammate's freshly-pushed branch isn't silently forked locally. Use --no-fetch when offline.
- Auto-copy beats manual copying. Configure
autoCopyFiles once in gw init; .env, secrets, etc. land in every new worktree.
- Post-checkout hooks beat manual installs. Wire
pnpm install / equivalent so each worktree is ready immediately.
- Shell integration is required for
cd. Without eval "$(gw install-shell)" in ~/.zshrc, gw cd and auto-nav after gw checkout silently no-op.
Typical Worktree Workflow
# 1. First-time repo setup (one-time per project)
gw init git@github.com:org/repo.git \
--auto-copy-files .env,.env.local \
--post-checkout "pnpm install" \
--interactive
# 2. Start a feature
gw checkout feat/my-feature
# - creates worktree, copies .env, runs pnpm install, auto-navigates
# 3. Work, then update with main
gw update
# 4. Open a PR for review (in another terminal / worktree)
gw pr 456
# - fetches PR branch, creates worktree, copies .env, auto-navigates
# 5. Navigate between worktrees
gw cd feat/my-feature
gw checkout main # navigates to main worktree (already checked out)
# 6. Extract staged WIP to a new branch
git add src/new-thing.ts
gw checkout feat/extracted --from-staged
# 7. Clean up when done
gw remove feat/my-feature # removes worktree + local branch
gw clean # batch remove safe (committed + pushed) worktrees
gw prune # full cleanup: worktrees + orphan branches
Config Reference
Config lives at .gw/config.json (committable) and .gw/config.local.json (gitignored, personal overrides):
{
"defaultBranch": "main",
"autoCopyFiles": [".env", ".env.local", "secrets/"],
"hooks": {
"checkout": {
"pre": ["echo 'Creating: {worktree}'"],
"post": ["pnpm install"],
},
},
"cleanThreshold": 7, // days before worktrees are "stale" for gw clean
"autoClean": true, // silently prune stale worktrees on gw checkout / gw list
"updateStrategy": "merge", // "merge" | "rebase"
"protectedBranches": ["staging"], // extra branches exempt from gw clean / auto-clean
}
Hook variables: {worktree}, {worktreePath}, {gitRoot}, {branch}.
Related Skills
Resources
1---2name: git-worktree-workflows3description: Use the `gw` CLI for ALL Git worktree work — creating, navigating, listing, removing, syncing, updating, checking out PRs, troubleshooting. Replaces raw `git worktree`, `cd ../wt`, `git checkout -b`, and manual file copies. Triggers on: "spin up a branch", "work on a feature", "check out PR", "switch branch without stashing", "create a worktree", "parallel branches", "clean up branches", "gw", "gw add", "gw checkout", "git worktree", or any branch workflow.4license: MIT5---67# Git Worktree Workflows89Master Git worktrees using the `gw` CLI tool for optimized parallel development workflows.1011## MANDATORY: Always Use `gw`, Never Raw `git worktree`1213**Non-negotiable in any repo where `gw` is installed.** Check with `which gw`; if missing, suggest `brew install mthines/gw-tools/gw` or `npm install -g @gw-tools/gw`.1415| Instead of | Use | Why it matters |16| ---------------------------------------------- | -------------------------------------- | ----------------------------------------------------------- |17| `git worktree add ../feat feat` | `gw checkout feat` | Auto-copies files, runs hooks, remote probe, auto-cd |18| `git worktree remove feat` | `gw remove feat` | Prunes orphan branch, protects `main` |19| `git checkout -b feat` | `gw checkout feat` | Isolated worktree instead of in-place branch switch |20| `git checkout main` (when main is another wt) | `gw checkout main` | Detects existing worktree and navigates instead of erroring |21| `cd ../feat` | `gw cd feat` | Partial matching, works from any depth |22| `git fetch && git checkout pr-branch` | `gw pr <number>` | Handles fork PRs, auto-copies, runs hooks |23| Manually `cp ../main/.env .env` after worktree | Configure `autoCopyFiles` in `gw init` | One-time setup, auto-copies forever after |2425## Rules2627| Rule | Description |28| --------------------------------------------- | ---------------------------------------------------------------------------------------- |29| [fundamentals](./rules/fundamentals.md) | **HIGH** - Core concepts of Git worktrees, what they share/don't share, when to use them |30| [creation](./rules/creation.md) | **HIGH** - Creating worktrees with `gw checkout`, remote fetch behavior, auto-copy files |31| [navigation](./rules/navigation.md) | **MEDIUM** - Navigating with `gw cd` and `gw checkout`, shell integration setup |32| [inspection](./rules/inspection.md) | **LOW** - Listing worktrees with `gw list`, understanding worktree states |33| [cleanup](./rules/cleanup.md) | **MEDIUM** - Removing worktrees, `gw clean`, `gw prune`, disk space management |34| [troubleshooting](./rules/troubleshooting.md) | **HIGH** - Common errors and solutions, recovery procedures |3536## Workflow Patterns3738| Pattern | Description |39| -------------------------------------------------------- | ------------------------------------------------------------- |40| [feature-branch](./rules/patterns/feature-branch.md) | **HIGH** - Feature development workflow with worktrees |41| [hotfix](./rules/patterns/hotfix.md) | **HIGH** - Urgent bug fixes without interrupting feature work |42| [code-review](./rules/patterns/code-review.md) | **HIGH** - Review PRs in isolated environments with `gw pr` |43| [parallel-testing](./rules/patterns/parallel-testing.md) | **MEDIUM** - Test across Node versions or configurations |4445## Quick Reference4647### Primary Commands4849| Task | Command |50| ----------------------------------------- | ------------------------------------------------------------------- |51| Create worktree (new branch) | `gw checkout feat/name` |52| Create worktree (alias) | `gw co feat/name` or `gw add feat/name` |53| Create from different branch | `gw checkout feat/name --from develop` |54| Create from staged files (extract WIP) | `gw checkout feat/name --from-staged` |55| Create without auto-navigation | `gw checkout feat/name --no-cd` |56| Skip remote probe (offline mode) | `gw checkout feat/name --no-fetch` |57| Skip pre/post-checkout hooks | `gw checkout feat/name --no-hooks` |58| Navigate to worktree by name | `gw cd feat/name` |59| Navigate by partial match | `gw cd feat` (matches first worktree with "feat") |60| Navigate to branch (even if in other wt) | `gw checkout main` |61| List all worktrees | `gw list` |62| Check out a PR into a worktree | `gw pr 123` |63| Check out a PR by URL | `gw pr https://github.com/user/repo/pull/123` |64| Update with main (merge or rebase) | `gw update` |65| Update from specific branch | `gw update --from develop` |66| Sync auto-copy files to current worktree | `gw sync` |67| Sync specific files | `gw sync .env .env.local` |68| Remove worktree + delete local branch | `gw remove feat/name` |69| Remove worktree but keep branch | `gw remove feat/name --preserve-branch` |70| Remove multiple worktrees by name | `gw remove feat-a feat-b feat-c` |71| Remove every worktree under a scope | `gw remove feat/*` (with shell integration) or `gw remove 'feat/*'` |72| Remove every worktree starting with name | `gw remove fix*` (greedy across `/`) |73| Preview a destructive `gw remove` | `gw remove --dry-run feat/*` |74| Batch remove safe (committed, pushed) wts | `gw clean` |75| Preview batch cleanup | `gw clean --dry-run` |76| Full cleanup: worktrees + orphan branches | `gw prune` |77| Get repo root path (worktree-aware) | `gw root` |78| Protect branch from cleanup | `gw protect [branch]` (default: cwd branch) |79| Remove protection from branch | `gw unprotect [branch]` (default: cwd branch) |8081### Setup Commands8283| Task | Command |84| --------------------------------------------- | --------------------------------------------------------------- |85| Clone repo and configure gw (interactive) | `gw init git@github.com:user/repo.git --interactive` |86| Init gw in existing repo | `gw init --auto-copy-files .env --post-checkout "pnpm install"` |87| Show current gw config | `gw show-init` |88| Install shell integration (required for `cd`) | `eval "$(gw install-shell)"` (add to `~/.zshrc`) |89| Remove shell integration | `gw install-shell --remove` |9091### Proxy Commands (pass-through to `git worktree`)9293| Task | Command |94| -------------------- | ---------------------------- |95| Lock worktree | `gw lock feat/name` |96| Unlock worktree | `gw unlock feat/name` |97| Repair worktree | `gw repair` |98| Move worktree | `gw move feat/name new-path` |99| Prune stale metadata | `gw prune --stale-only` |100101## Key Principles102103- **`gw checkout` is canonical.** `gw add` and `gw co` are aliases — prefer `gw checkout` in explanations.104- **One branch per worktree.** Same branch cannot be checked out in two worktrees. `gw checkout <existing>` detects this and navigates instead.105- **Remote probe before treating a branch as new.** `gw checkout` runs `git ls-remote` (3s timeout) so a teammate's freshly-pushed branch isn't silently forked locally. Use `--no-fetch` when offline.106- **Auto-copy beats manual copying.** Configure `autoCopyFiles` once in `gw init`; `.env`, secrets, etc. land in every new worktree.107- **Post-checkout hooks beat manual installs.** Wire `pnpm install` / equivalent so each worktree is ready immediately.108- **Shell integration is required for `cd`.** Without `eval "$(gw install-shell)"` in `~/.zshrc`, `gw cd` and auto-nav after `gw checkout` silently no-op.109110## Typical Worktree Workflow111112```bash113# 1. First-time repo setup (one-time per project)114gw init git@github.com:org/repo.git \115 --auto-copy-files .env,.env.local \116 --post-checkout "pnpm install" \117 --interactive118119# 2. Start a feature120gw checkout feat/my-feature121# - creates worktree, copies .env, runs pnpm install, auto-navigates122123# 3. Work, then update with main124gw update125126# 4. Open a PR for review (in another terminal / worktree)127gw pr 456128# - fetches PR branch, creates worktree, copies .env, auto-navigates129130# 5. Navigate between worktrees131gw cd feat/my-feature132gw checkout main # navigates to main worktree (already checked out)133134# 6. Extract staged WIP to a new branch135git add src/new-thing.ts136gw checkout feat/extracted --from-staged137138# 7. Clean up when done139gw remove feat/my-feature # removes worktree + local branch140gw clean # batch remove safe (committed + pushed) worktrees141gw prune # full cleanup: worktrees + orphan branches142```143144## Config Reference145146Config lives at `.gw/config.json` (committable) and `.gw/config.local.json` (gitignored, personal overrides):147148```jsonc149{150 "defaultBranch": "main",151 "autoCopyFiles": [".env", ".env.local", "secrets/"],152 "hooks": {153 "checkout": {154 "pre": ["echo 'Creating: {worktree}'"],155 "post": ["pnpm install"],156 },157 },158 "cleanThreshold": 7, // days before worktrees are "stale" for gw clean159 "autoClean": true, // silently prune stale worktrees on gw checkout / gw list160 "updateStrategy": "merge", // "merge" | "rebase"161 "protectedBranches": ["staging"], // extra branches exempt from gw clean / auto-clean162}163```164165Hook variables: `{worktree}`, `{worktreePath}`, `{gitRoot}`, `{branch}`.166167## Related Skills168169- [gw-config-management](../gw-config-management/) - Configure auto-copy files and hooks170- [autonomous-workflow](https://github.com/mthines/agent-skills#autonomous-workflow) - Autonomous development in isolated worktrees (lives in `mthines/agent-skills`)171172## Resources173174- [Getting Started Example](./references/getting-started.md)175- [Parallel Development Example](./references/parallel-development.md)176- [Troubleshooting Guide](./references/troubleshooting-worktrees.md)177- [gw CLI Documentation](../../packages/gw-tool/README.md)