# Git Worktrees Worktrees let you check out multiple branches simultaneously in separate directories, all backed by the same `.git` database. No stashing, no cloning — just `cd` between branches. ## Basic Commands ```bash git worktree add ../feature-branch feature-branch # check out branch in sibling dir git worktree add -b new-branch ../new-branch # create + check out new branch git worktree list # show all worktrees git worktree remove ../feature-branch # clean up ``` Each worktree is a real working directory with its own index and `HEAD`. Commits, stashes, and reflog are shared across all worktrees. ## Oh-My-Zsh Aliases The [[Oh-My-Zsh Git Aliases|git plugin]] includes worktree aliases: | Alias | Command | |-------|---------| | `gwta` | `git worktree add` | | `gwtls` | `git worktree list` | | `gwtrm` | `git worktree remove` | ## Fuzzy Switching with fzf There's no built-in way to `cd` into a worktree by name. A small shell function with [[fzf]] handles it: ```bash # ~/.zshrc gw() { local dir=$(git worktree list | fzf --query="$1" | awk '{print $1}') [ -n "$dir" ] && cd "$dir" } ``` Run `gw` to fuzzy-pick from all worktrees, or `gw feat` to pre-filter. ## When to Use - **Parallel work**: review a PR while your branch keeps compiling - **Long-running tasks**: keep a build running on `main` while you hack on a feature - **Comparing branches**: open two editors side by side, each on a different branch - **CI/CD locally**: test the release branch without disrupting your dev branch ## Gotchas - You **cannot** check out the same branch in two worktrees - Deleting a worktree directory manually leaves stale entries — always use `git worktree remove` or `git worktree prune` - Submodules need extra care — they're not automatically initialized in new worktrees