Sub topic 3
Workflows: urgent fix, code review, and long-running builds
Assume you know commits, branches, remotes, fetching, and the difference between the working directory, index, and repository history.
Choose a worktree for each task
A worktree gives one repository several working directories. This avoids stashing a feature while handling another branch.
# Create a hotfix from main
git worktree add ../myproject-hotfix -b hotfix/login-crash main
cd ../myproject-hotfix
git push -u origin hotfix/login-crash
# Review a remote branch without checking out its branch name
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchUse a separate worktree for a long-running build or migration. Your main worktree remains available for normal development.
Untracked files, ignored files, and dependencies
Commits and most repository history are shared, but each worktree has its own checked-out files, index, HEAD, and untracked files. Creating a file in one worktree does not create it in another. Ignored files are also local.
Dependency directories such as node_modules or virtual environments usually need separate setup. They may consume substantial disk space.
Locking, moving, and repairing
A removable drive or inactive directory can be locked so Git does not prune it. Move worktrees with Git when possible. If a directory was moved manually, repair the links.
git worktree lock --reason "on removable drive" ../myproject-review
git worktree move ../myproject-hotfix ../work/myproject-hotfix
git worktree repairBare repository layout
A bare clone stores Git history without a default working directory. You can place every branch in a sibling worktree.
git clone --bare https://example.com/team/myproject.git myproject.git
cd myproject.git
git worktree add ../main main
git worktree add ../feature-x -b feature-x mainLimitations and caveats
A branch can normally be checked out in only one worktree. Use a detached worktree for a second read-only view, or create another branch. Worktrees do not isolate shared commits, refs, or remote operations. Remove clean worktrees before deleting their directories, then prune stale metadata.
Exercise
You need to review origin/feature/search while keeping feature/search checked out elsewhere. Which command should you use?
Show answer
Run git fetch origin followed by git worktree add --detach ../review origin/feature/search.