Multi-Worktree Development¶
One git worktree per coding task; the main worktree only integrates.
This page describes the multi-worktree (git worktree) workflow used in the DAWidgets repository: where a task is carried out, where its build directory comes from, and how the finished branch is merged back to master and cleaned up. The same rules live in the "Branch & Parallel Development" section of AGENTS.md; this page is the developer-facing explanation.
Why worktrees¶
DAWidgets is often developed by several agent sessions or people in parallel. Sharing one working tree makes tasks pollute each other:
- switching branches carries away other people's uncommitted changes;
- there is only one staging area, so
git addeasily picks up unrelated edits; - a single
build/is rebuilt by different tasks, so artifacts and the CMake cache overwrite each other; - debugging means guessing which of the mixed edits belong to the current task.
A worktree gives every task its own branch checkout and build directory, while the main worktree stays on master and is always ready to integrate a finished branch.
Branch model¶
masteris the integration branch. There is no separatedevhere: data-workbench'sdevalready plays that role and consumes this repository as a submodule.- Task branches are short-lived:
feat/<domain>-<topic>,fix/<topic>,docs/<topic>,chore/<topic>; delete them after merging back tomaster. - A worktree always lives at
<main worktree>/.worktree/<branch with / replaced by ->, which is git-ignored.
flowchart LR
M[master main worktree] -->|cut task branch| W[.worktree/feat-xxx]
W --> C[code + build + test]
C -->|merge| M
M -->|delete branch and worktree| M
Create a worktree¶
1 2 3 4 5 | |
If the PowerShell execution policy blocks the script, use:
1 | |
| Parameter | Description |
|---|---|
<branch> |
Required. Checked out if it already exists, otherwise created from the base branch |
[base] |
Optional, defaults to master |
-FromBase <dir> |
Build directory used as the configuration template (e.g. build-tests, build-qt5) |
-NoConfigure |
Create the worktree only, without preconfiguring build/ |
-List |
List all worktrees (* marks the main worktree) |
-Remove <dir-name> |
Remove a worktree and keep its branch |
-Help |
Print usage |
The script does more than git worktree add:
- Resolves the main worktree from wherever it is called, so new worktrees always land under the main worktree's
.worktree/and never nest inside another worktree. - Preconfigures the build directory by reading the generator, VS instance,
CMAKE_PREFIX_PATHandDAWIDGETS_BUILD_*values from the main worktree'sbuild*/CMakeCache.txt; caches withDAWIDGETS_BUILD_TESTS=ONare preferred soctestworks out of the box. - Refuses submodule checkouts: running it in data-workbench's
src/3rdparty/DAWidgetsfails immediately — that path must follow data-workbench's four-step flow. - Removes safely: on Windows, build artifacts may be locked, and a failed
git worktree removeleaves a de-registered shell directory;-Removeconfirms git has de-registered the tree before retrying and refuses to delete the main worktree or unregistered directories.
First build
A new worktree has no reusable build directory, so the first build is a full build; later builds are incremental.
Non-Windows platforms
The script targets this repository's verified environment (MSVC/Windows). It works on Linux/macOS with PowerShell 7 installed; otherwise perform the equivalent steps by hand:
1 2 3 | |
Remove it with git worktree remove --force .worktree/feat-xxx.
Work inside the worktree¶
Build, test and run the demo exactly as in the main worktree (the script prints the inherited build directory):
1 2 3 | |
The preconfigured build/ inherits one Qt/option set. To verify both Qt5 and Qt6 in the same task, add another build directory by hand:
1 | |
Do not run cmake --install from a worktree
The install prefix is shared/global and not owned by a task. Install from the main worktree when needed (for example, for data-workbench's 3rdparty tree).
Finish: merge back and clean up¶
- Merge the latest
masterinto the task branch when starting and again before finishing, so conflicts surface early. - Rebuild and rerun the module tests in the task worktree — they are the quality gate.
- Merge from the main worktree:
git -C <main worktree> merge feat/table-column-picker. Ifmasteris checked out in another worktree with in-flight edits, use a temporary worktree; never switch away from or clean someone else's tree. - Delete the branch and the worktree:
1 2 | |
"Task done" includes "merged back to master and the worktree removed." Do not leave conflicts, diverged branches or orphan worktrees for the next task.
Relationship to data-workbench¶
When DAWidgets is changed from inside data-workbench (src/3rdparty/DAWidgets), that project's rules win: edit the submodule in its main worktree, commit it, rebuild 3rdparty and cmake --install there. Do not create a nested worktree inside that submodule checkout; this repository's script refuses to.
Troubleshooting¶
| Symptom | Cause and handling |
|---|---|
-Remove says "not a registered worktree" |
The directory is not a worktree, or a previous removal left a shell; empty directories are cleaned automatically, non-empty ones need manual review |
| The worktree directory cannot be deleted | An editor/terminal/MSBuild still has it as its current directory; close it and retry — git has usually de-registered the tree already |
ctest finds no tests in the new worktree |
The template cache has DAWIDGETS_BUILD_TESTS=OFF; use -FromBase build-tests or add -DDAWIDGETS_BUILD_TESTS=ON manually |
| CMake cannot find Qt | Wrong template or Qt was uninstalled; use -FromBase <correct build dir> or pass -DCMAKE_PREFIX_PATH manually |
| The script refuses to run in a submodule | That is data-workbench's flow; follow its four-step workflow in the main worktree |
References¶
- The "Branch & Parallel Development (Git Worktrees)" section in the repository's
AGENTS.md - Worktree script:
scripts/worktree-add.ps1 - data-workbench's multi-worktree practice (its
scripts/worktree-add.sh)