🔀 Git & GitHub Intermediate

What is git worktree?

Answer

Git worktrees allow you to check out multiple branches simultaneously in separate directories, all sharing the same repository data. Each worktree has its own working directory and index. Without worktrees, switching branches means stashing or committing your current work. With worktrees, you can work on multiple branches concurrently. Commands: git worktree add ../hotfix hotfix-branch — check out hotfix-branch into a sibling directory ../hotfix; git worktree add -b new-feature ../feature — create and check out a new branch; git worktree list — list all worktrees; git worktree remove ../hotfix — remove a worktree; git worktree prune — clean up stale worktree metadata. Use cases: (1) Urgent hotfix while mid-feature — add a hotfix worktree, fix, build, test, push — without disturbing feature work; (2) Running tests on another branch while continuing to develop; (3) Comparing behavior between branches; (4) Long-running builds in a separate worktree. Constraints: you can't check out the same branch in two worktrees simultaneously. Each worktree uses minimal extra disk space (shared object store, just different working files).