Sub topic 8
Workflows: urgent fix, code review and long-running builds
This chapter applies linked worktrees to three jobs that normally force you to stash, commit half-finished work, or clone the repository again. It assumes you can commit, branch, merge and fetch, and that you know the difference between the working directory, the index and the repository history. It also assumes you have already created a worktree with git worktree add and know that a branch can be checked out in only one worktree at a time.
Why these three jobs
Each job interrupts the work you already have open:
- An urgent fix must start from a clean copy of a release branch while your feature is half done.
- A code review needs the other person's code on disk so you can run it.
- A long-running build or test suite occupies a working directory for minutes or hours, and you do not want to stop editing while it runs.
A linked worktree gives each job its own directory, index and HEAD, while all of them share one object database. Nothing is copied, and there is nothing to stash.
Workflow 1: the urgent fix
You are halfway through a feature in ~/code/myproject on branch feature/search. Your working directory has modified and untracked files. A bug report arrives: login crashes in production, and the fix must start from main.
Without worktrees you would run git stash, switch to main, fix, switch back and run git stash pop. That sequence is easy to get wrong, and stashes can conflict when you pop them. With a worktree, you leave your feature directory alone.
git fetch origin
git worktree add ../myproject-hotfix -b hotfix/login-crash origin/main
cd ../myproject-hotfixThis command creates the directory ../myproject-hotfix, creates the new branch hotfix/login-crash from origin/main, and checks the branch out there. Starting from origin/main after a fetch means the fix is based on what is actually deployed, even if your local main is behind.
Now work normally in the new directory:
git status
# edit the file that crashes
git add src/login.py
git commit -m "Fix crash when the session token is missing"
git push -u origin hotfix/login-crashYour feature directory is untouched. Its modified files, its index and its untracked files are exactly as you left them. When the fix is merged, clean up:
cd ../myproject
git worktree remove ../myproject-hotfix
git branch -d hotfix/login-crashThe git branch -d step is separate because removing a worktree never deletes its branch. The -d flag refuses to delete a branch that has unmerged commits, which protects you if the fix was not merged yet.
If the hotfix needs your feature work
Sometimes the fix must also appear in your feature branch. Do not copy files between directories. Commit the fix once, then bring it in with ordinary Git commands from the feature directory:
cd ../myproject
git cherry-pick hotfix/login-crashBecause the commit objects are shared, the commit is already available in the feature directory the moment you create it in the hotfix worktree. No fetch is needed between worktrees.
Workflow 2: code review
Reviewing a pull request usually means running the code, not only reading a diff. A detached worktree gives you a throwaway copy that cannot interfere with anything.
git fetch origin
git worktree add --detach ../myproject-review origin/feature/search
cd ../myproject-review--detach checks out the commit itself rather than a branch. That has two benefits. First, no local branch name is needed, so you do not have to invent one. Second, the one-branch-per-worktree rule does not apply, so you can review feature/search even if the author's branch name is already checked out in another of your worktrees.
Run the tests and try the change:
git log --oneline main..HEAD
git diff main...HEAD --stat
python -m pytestIf you want to leave a small fix suggestion, you can commit in this worktree. A commit made on a detached HEAD belongs to no branch, so give it a branch before you leave, or it becomes unreachable and will eventually be garbage collected:
git switch -c review/search-suggestionWhen the author pushes new commits, you do not need to remove and re-create the worktree. Update it in place:
git fetch origin
git checkout --detach origin/feature/searchRun this inside the review worktree. It moves the detached HEAD to the new tip of the remote branch. If you made local edits that conflict with the new commit, Git refuses and tells you which files are in the way.
Reviewing several pull requests at once
Because each worktree is cheap, you can keep one per open review and compare them side by side:
git worktree add --detach ../review-pr-101 origin/pr/101
git worktree add --detach ../review-pr-102 origin/pr/102
git worktree listThe origin/pr/... names here are only an example. Use whatever remote-tracking names your hosting service and fetch configuration produce.
Workflow 3: long-running builds and tests
Some jobs take a long time: a full integration test run, a release build, a benchmark, or a migration rehearsal. If you start them in your main directory, any edit you make while they run can change the result. A compiler or test runner reads files as it goes, so editing mid-run produces a build that matches neither the old nor the new code.
A dedicated worktree pins the code under test:
git worktree add --detach ../myproject-build HEAD
cd ../myproject-build
make test > ../build-output.log 2>&1The build worktree is at the commit you named, and no one edits files there. You can keep working in your main directory and the results still describe a known commit. When the run finishes, read the log and remove the worktree.
You can also run the same test suite against two commits in parallel to find where a regression began:
git worktree add --detach ../bisect-good v2.3.0
git worktree add --detach ../bisect-bad HEADRun the failing test in each directory and compare the output. For a systematic search, git bisect also works inside a worktree, and it keeps its own state per worktree, so a bisect in one directory does not disturb another.
Comparing the three workflows
| Job | Start point | Branch or detached | Typical lifetime | Remove when |
|---|---|---|---|---|
| Urgent fix | `origin/main` after a fetch | New branch, `-b hotfix/...` | Hours | Fix is merged |
| Code review | `origin/<branch>` | Detached | Minutes to days | Review is done |
| Long-running build | A specific commit or tag | Detached | Minutes to hours | Results are saved |
Common errors in these workflows
- "fatal: 'feature/search' is already checked out at ..." You tried to check out a branch that another worktree holds. Either use
--detachfor read-only work, or switch the other worktree to a different branch first. Rungit worktree listto see which directory holds the branch. - "fatal: a branch named 'hotfix/login-crash' already exists" A previous hotfix branch was never deleted. Delete it with
git branch -d, or check out the existing branch without-b. - "fatal: '../myproject-hotfix' already exists" The target directory exists and is not empty. Pick another path, or remove the old directory and run
git worktree pruneif it was deleted by hand. - Hotfix is based on stale code. You created it from local
mainwithout fetching. Fetch first and start fromorigin/main. - Build output is missing in a new worktree. Ignored and untracked files are not copied. Run the install or build step again in the new directory.
git worktree removerefuses with "contains modified or untracked files". The worktree is not clean. Commit or discard the changes, or pass--forceonly when you are sure the files can be lost.
Keeping the habit tidy
Temporary worktrees accumulate if you do not remove them. A short routine helps:
- Run
git worktree listat the start of each day. - Remove worktrees whose purpose is finished with
git worktree remove. - Run
git worktree pruneif you deleted any directories by hand. - Delete merged branches with
git branch -d.
Exercise
You are on feature/search with uncommitted edits. A bug on main must be fixed today, and a teammate also asked you to run the tests of their branch origin/feature/billing. Give the commands that create one worktree for each job without touching your current directory, and say which of the two should be detached and why.
Show answer
Hotfix: git fetch origin then git worktree add ../myproject-hotfix -b hotfix/billing-rounding origin/main. The hotfix needs a branch because you will commit and push it. Review: git worktree add --detach ../myproject-review origin/feature/billing. The review worktree should be detached because you only read and run the code, so you need no local branch, and the one-branch-per-worktree rule cannot block you. If you commit in the review worktree, create a branch with git switch -c before removing it.
Exercise
You start make test in your main directory, which takes 40 minutes, and keep editing source files while it runs. Why can the result be misleading, and what is the worktree-based fix?
Show answer
The test run reads files while it executes, so edits made during the run mix old and new code, and the result matches no single commit. Create a detached worktree at the commit you want to test with git worktree add --detach ../myproject-build HEAD, run the tests there, and keep editing in the main directory. Remove the worktree with git worktree remove ../myproject-build when the run is finished.