|
| 1 | +# git-work |
| 2 | + |
| 3 | +`git-work` is a CLI that wraps `git worktree` into a branch-per-directory workflow. Each branch lives in its own subdirectory. Context-switching is `cd`, not `git checkout`. |
| 4 | + |
| 5 | +## Layout |
| 6 | + |
| 7 | +``` |
| 8 | +my-project/ |
| 9 | +├── .bare/ # bare git repo shared by all worktrees |
| 10 | +├── .git # pointer file: gitdir: ./.bare |
| 11 | +├── main/ # worktree for branch main |
| 12 | +├── feature-login/ # worktree for branch feature/login |
| 13 | +└── fix-typo/ # worktree for branch fix-typo |
| 14 | +``` |
| 15 | + |
| 16 | +`/` in branch names is replaced with `-` for the directory name. The real branch name is always used in git commands. |
| 17 | + |
| 18 | +## Shell wrapper (`gw`) |
| 19 | + |
| 20 | +The `gw` shell function wraps `git-work` and automatically `cd`s into any path printed to stdout. Set it up once per shell: |
| 21 | + |
| 22 | +```bash |
| 23 | +# bash / zsh |
| 24 | +eval "$(git-work activate bash)" |
| 25 | + |
| 26 | +# fish |
| 27 | +git-work activate fish | source |
| 28 | +``` |
| 29 | + |
| 30 | +Use `gw <command>` in interactive shells. Call `git-work <command>` directly in scripts (no `cd` side-effect). |
| 31 | + |
| 32 | +## stdout / stderr contract |
| 33 | + |
| 34 | +**stdout** is machine-readable only — directory paths. All human-readable messages, warnings, and errors go to **stderr**. This is what allows `gw` to `cd` reliably. |
| 35 | + |
| 36 | +## Commands |
| 37 | + |
| 38 | +### Start a project |
| 39 | + |
| 40 | +```bash |
| 41 | +# Clone a remote repo into git-work layout |
| 42 | +gw clone git@github.com:org/repo.git |
| 43 | +gw clone https://github.com/org/repo.git my-project |
| 44 | + |
| 45 | +# Convert an existing normal repo in-place (run from inside repo root) |
| 46 | +cd ~/projects/my-repo |
| 47 | +gw init |
| 48 | +``` |
| 49 | + |
| 50 | +`clone` creates `<dir>/.bare` (bare repo), writes `<dir>/.git` pointer, and adds a worktree for HEAD. |
| 51 | + |
| 52 | +`init` moves `.git/` → `.bare/`, moves working files into `<root>/<current-branch>/`, wires up worktree linkage, and stashes/restores uncommitted changes. Safe to re-run: repairs state if the layout already exists but is broken. |
| 53 | + |
| 54 | +### Navigate between branches |
| 55 | + |
| 56 | +```bash |
| 57 | +gw co main # switch to existing worktree by name |
| 58 | +gw co login # fuzzy match — resolves to feature-login |
| 59 | +gw co - # switch to previously visited worktree |
| 60 | +gw co feature/remote # auto-create worktree from remote branch (no -b needed) |
| 61 | +gw co -b feature/new # create new worktree + branch |
| 62 | +``` |
| 63 | + |
| 64 | +Fuzzy matching order: |
| 65 | +1. Exact directory name match |
| 66 | +2. Substring match (`login` → `feature-login`) |
| 67 | +3. Jaro-Winkler similarity, threshold 0.85 |
| 68 | + |
| 69 | +Ambiguous matches (multiple candidates pass) exit non-zero and list candidates on stderr. |
| 70 | + |
| 71 | +`gw co <branch>` without `-b` will auto-create a worktree only if a remote branch with that **exact** name exists. Otherwise it errors. |
| 72 | + |
| 73 | +### Remove a worktree |
| 74 | + |
| 75 | +```bash |
| 76 | +gw rm feature-login # prompts for confirmation |
| 77 | +gw rm login # fuzzy match |
| 78 | +gw rm --yes feature-login # skip confirmation |
| 79 | +gw rm --force old-branch # force-delete (unmerged ok; HEAD branch ok) |
| 80 | +``` |
| 81 | + |
| 82 | +Removes the worktree directory and runs `git branch -d` (or `-D` with `--force`). Refuses to remove HEAD branch without `--force`. If you run `gw rm` from inside the worktree being removed, the tool prints the HEAD worktree path so the shell wrapper moves you there. |
| 83 | + |
| 84 | +### Sync with remote |
| 85 | + |
| 86 | +```bash |
| 87 | +gw sync # fetch --all --prune, then remove stale local worktrees |
| 88 | +gw sync --dry-run # preview what would be pruned |
| 89 | +gw sync --force # force-delete branches with unmerged changes |
| 90 | +``` |
| 91 | + |
| 92 | +HEAD branch is never pruned. |
| 93 | + |
| 94 | +### List worktrees |
| 95 | + |
| 96 | +```bash |
| 97 | +gw ls |
| 98 | +``` |
| 99 | + |
| 100 | +Prints a table: directory name, branch name, `*` for current worktree. |
| 101 | + |
| 102 | +## Hooks (mise integration) |
| 103 | + |
| 104 | +On every new worktree creation (`gw co -b` or remote auto-create), git-work fires a post-create hook: |
| 105 | + |
| 106 | +1. If the source worktree is `mise`-trusted, runs `mise trust` in the new worktree. |
| 107 | +2. Runs `mise run worktree-setup` in the new worktree if the task exists. |
| 108 | + |
| 109 | +If the task does not exist, a warning is printed and execution continues. If the task exits non-zero, worktree creation is rolled back. |
| 110 | + |
| 111 | +Configure via git config (stored in `.bare`): |
| 112 | + |
| 113 | +```bash |
| 114 | +# disable trust propagation |
| 115 | +git -C .bare config git-work.hooks.mise.trust false |
| 116 | + |
| 117 | +# use a custom task name |
| 118 | +git -C .bare config git-work.hooks.mise.task bootstrap |
| 119 | + |
| 120 | +# disable task execution entirely |
| 121 | +git -C .bare config git-work.hooks.mise.task "" |
| 122 | +``` |
| 123 | + |
| 124 | +## Common patterns for agents |
| 125 | + |
| 126 | +**Determine current worktree and project root:** |
| 127 | +```bash |
| 128 | +pwd # current worktree dir, e.g. /home/user/repo/feature-login |
| 129 | +git -C .bare rev-parse --show-toplevel # not useful; use filesystem layout instead |
| 130 | +``` |
| 131 | +The project root is the parent of the current worktree directory. `.bare/` always lives at the project root. |
| 132 | + |
| 133 | +**Check what worktrees exist:** |
| 134 | +```bash |
| 135 | +gw ls |
| 136 | +# or directly: |
| 137 | +git -C ../.bare worktree list |
| 138 | +``` |
| 139 | + |
| 140 | +**Create a branch and immediately work in it:** |
| 141 | +```bash |
| 142 | +gw co -b feature/my-task |
| 143 | +# shell wrapper cd'd you in; now you are in the new worktree |
| 144 | +``` |
| 145 | + |
| 146 | +**Clean up after merging:** |
| 147 | +```bash |
| 148 | +gw rm feature/my-task --yes |
| 149 | +# or let sync handle it after remote branch is deleted |
| 150 | +gw sync |
| 151 | +``` |
| 152 | + |
| 153 | +**Check if inside a git-work project:** |
| 154 | +A git-work project has `.bare/` at its root (one level up from the current worktree). If `../.bare` is a bare git repo, you are inside a git-work layout. |
| 155 | + |
| 156 | +## Error cases |
| 157 | + |
| 158 | +| Situation | Behavior | |
| 159 | +|---|---| |
| 160 | +| `gw co <branch>` — no worktree, no remote | exits non-zero, error on stderr | |
| 161 | +| `gw co <branch>` — multiple fuzzy matches | exits non-zero, candidates on stderr | |
| 162 | +| `gw co -b <branch>` — worktree already exists | exits non-zero | |
| 163 | +| `gw rm <head-branch>` without `--force` | exits non-zero | |
| 164 | +| mise task exits non-zero | worktree creation rolled back, exits non-zero | |
| 165 | +| Not inside a git-work project | exits non-zero | |
0 commit comments