Sub topic 8
Workflows: urgent fix, code review and long-running builds
This chapter assumes you can commit, branch and merge in Git, and that you know how to create, list and remove linked worktrees. It also assumes you know what git fetch does and what a remote-tracking branch is. Here you apply worktrees to three everyday jobs.
Why these three jobs
Each job has the same shape. You are in the middle of something, and another task needs a different branch right now. Without worktrees you would stash, switch, work and switch back. With a worktree, your current directory stays untouched.
Urgent fix without stashing
You are halfway through a change on feature/search when a login crash is reported. Leave your files alone and create a new branch from main in a sibling directory:
git worktree add ../myproject-hotfix -b hotfix/login-crash main
cd ../myproject-hotfixFix the bug, run the tests, and commit as usual. Then push the branch and open your merge request:
git commit -am "Fix crash when the session token is missing"
git push -u origin hotfix/login-crashYour original directory still has its uncommitted edits, exactly as you left them. Once the fix is merged, clean up from the main directory:
cd ../myproject
git worktree remove ../myproject-hotfix
git branch -d hotfix/login-crashgit worktree remove refuses to delete a worktree with uncommitted changes. That is a useful safety check, so do not reach for --force until you have looked at what would be lost.
Reviewing someone else's branch
A reviewer wants to run the code, not only read the diff. Check out the branch at a detached HEAD, so no local branch is created:
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchDetaching has two benefits for review. It avoids the one-branch-per-worktree rule, so it works even if feature/search is already checked out elsewhere. It also signals that this directory is for reading and testing, not for new commits.
When the author pushes more commits, update the same worktree instead of creating a new one:
cd ../myproject-review
git fetch origin
git checkout --detach origin/feature/searchIf you do want to push a small suggestion, create a branch from here with git switch -c review/search-tweak.
Long-running builds and tests
Some tasks occupy a directory for minutes or hours: a full test suite, a large compile, a benchmark run. If you run them in your only working directory, you cannot safely edit files while they run, because the build may pick up half-finished changes.
Give the run its own worktree at a fixed commit:
git worktree add --detach ../myproject-build HEAD
cd ../myproject-build
make testThe build sees a stable snapshot. Meanwhile you keep editing in your main directory. Later commits do not affect the running build.
The same approach works for a long-lived experiment. Create a branch in its own worktree, and come back to it whenever you like. It keeps its own index and working files between sessions.
Common mistakes
- Forgetting dependencies. A new worktree has only tracked files. If your project needs
node_modules, a virtual environment or a.envfile, set them up again in the new directory. Chapter material on untracked files covers this in detail. - Reusing a checked-out branch.
git worktree add ../x feature/searchfails if that branch is already checked out. Use--detachor pick another branch. - Deleting the directory by hand. This leaves stale metadata behind. Run
git worktree remove, orgit worktree pruneafterwards. - Losing track of worktrees. Run
git worktree listregularly, and remove the ones you no longer need.
Choosing between the three patterns
| Job | Starting point | Branch? | Lifetime |
|---|---|---|---|
| Urgent fix | `main` with `-b` | New branch | Until merged |
| Code review | `origin/<branch>` with `--detach` | None | Until review ends |
| Long build | `HEAD` with `--detach` | None | Until the run finishes |
Exercise
You are editing on feature/search and a colleague asks you to run the tests on their pushed branch origin/fix/export. Which commands give you a separate directory for this without touching your current files, and why is --detach a good choice?
Show answer
Run git fetch origin, then git worktree add --detach ../myproject-review origin/fix/export. Your current directory is untouched. --detach avoids creating a local branch, so the one-branch-per-worktree rule cannot block you. It also marks the directory as read-and-test only. Remove it afterwards with git worktree remove ../myproject-review.
Exercise
A long test run is in progress in ../myproject-build. You delete that directory with rm -rf after the run. What command cleans up Git's leftover metadata, and what should you have used instead?
Show answer
Run git worktree prune to delete the stale administrative entry. You should have used git worktree remove ../myproject-build, which removes both the directory and the metadata and refuses if there are uncommitted changes.