CodeRigor

FREE LESSON

Worktrees & parallel branch workflows

Work on separate branches in linked directories while preserving each worktree’s index and local edits.

A linked worktree gives another directory a checkout of the same repository. It is useful when a review, urgent fix or experiment needs its own working files while your current edits remain undisturbed. This is the workflow you can use to develop a feature alongside an existing C++ or Python checkout without continually switching one directory between tasks.

One repository, two worktrees
start / 3
Both directories use one common repository; their working files, indexes and HEADs remain separate.
One repository, two worktrees
Both directories use one common repository; their working files, indexes and HEADs remain separate.

Shared history, separate working state

Linked worktrees share the object database and most repository references. Each has its own working files, index and HEAD. Committing in feature/review advances that branch; it does not silently stage or commit the unfinished edits in the main worktree. The .git entry in a linked worktree is normally a file pointing to its administrative directory, not a second full repository directory.

Git commands
git worktree list
git worktree add -b feature/review ../review main
git worktree list
git status --short --branch

The -b form creates a branch at the given start point and checks it out in the new directory. Giving main as the start point chooses its committed snapshot; it does not copy unstaged edits from the current directory. Run subsequent development commands from the new directory in a real terminal. The lab command sequence runs from the main directory throughout, so it grades creation and isolation rather than asking you to use shell cd commands.

Preserve local work
start / 4
Preserve local work

Respect branch ownership

Git normally refuses to check out the same branch in two worktrees at once. If both indexes independently assumed they owned one moving branch, the state would be difficult to reason about. Create a different branch or use a detached checkout when you only need to inspect an exact revision. A detached worktree is useful for review, but it does not satisfy a task that explicitly asks for a named development branch.

git worktree remove <path> removes a clean linked worktree and updates its administrative records. Inspect uncommitted and untracked files before removal; forcing removal can discard work. git worktree prune cleans obsolete administrative records left by missing directories and is not the same operation as deleting a healthy checkout. For a deliberate move, use the worktree move command where supported rather than manually breaking its link.

Choose an isolation boundary
start / 4
Choose an isolation boundary

Worktree or clone?

A clone is another repository with its own refs and configuration, connected through fetch/push. A linked worktree shares the original repository. Choose a clone when you need that repository separation; choose a worktree when parallel checkouts of shared history are the goal. Neither automatically duplicates all ignored build artifacts or provides independent remote-branch ownership.

This lab checks the common Git directory as well as the visible branch. A separate repository that merely contains the same README will not pass. Inspect both the main state and the worktree list in Run sample: the unfinished main edit should remain, and the review checkout should start clean from committed main.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Review the object/reference model and core workflows here. The authored lesson and linked Git manual cover the current command forms and advanced details used in this lab.