Ray's Knowledge Base
Unlock

Split one large uncommitted change into feature commits that each build

RecipeVerified 30 Sep 2026Holds anywhere
Recipe. When to use it, the steps, and what showed that they work.

When to use#

The working tree holds several features at once, some files hold hunks of more than one feature, and each commit must build and pass the tests alone. Also use it to check a branch that was split by hunk: building only the final tree hides broken middle commits.

Steps#

  1. Snapshot the final tree without touching the index: git stash create gives a commit id. Save it.
  2. Classify each hunk of git diff -U0 HEAD <snapshot> to a feature, by file first, then by words in the hunk. Write the step trees (step 1, step 1+2, ...) to a scratch folder. Check that applying every hunk gives back the final file.
  3. For a file where two features touch the same lines, do not use hunks. Build its step versions from the final text with exact string replacements that take the later features out.
  4. Make a detached worktree at HEAD in the scratch folder. Symlink the untracked inputs the build needs (dependency cache, generated web assets, node_modules).
  5. Copy step 1 in. Build with ninja -k 0 so you see every error in one pass. Fix the step files by hand, then run the full test suite. A build that passes is not enough: a cut hunk can remove a return and leave code that compiles but crashes. Use a build dir of its own for the worktree, and run each commit's narrow tests.
  6. Commit in the worktree so the hooks check the real step content. If the formatter changes files, stage them and commit again.
  7. Repeat for each later step. The last step is the snapshot itself, without the files you must not commit.
  8. Compare the last commit with the main working tree: git diff --stat <last> must show only the files you left out. When you rebuilt an existing branch, git diff <old-tip> <new-tip> must be empty.
  9. Move the branch: git update-ref refs/heads/<branch> <last> <old HEAD>, then git reset (mixed) in the main tree. Remove the worktree. update-ref leaves the main working tree and uncommitted WIP untouched.

Evidence#

Seen in dftracer-utils (2026-09-29): 185 files split into 3 commits. A regex classifier cut hunks inside mixed files. Step 1 failed to build (no member named 'time' in 'dftu_window_spec', an unbalanced {, excess elements in struct initializer). After a fix it built, but test_concat crashed with SIGSEGV, because a later-feature hunk had changed return Series{ to Series out{ in concat_columns. After hand fixes each step passed 302 of 302 C++ tests, and the final commit differed from the working tree only in the excluded files.

An observer note from a later session (2026-09-30) saw the same failure in a branch already split by hunk: frame_to_native used one commit before the commit that adds it, src/CMakeLists.txt named a file not yet added, and a test hunk belonged to a later commit. Checking every commit in a scratch worktree found all three; the fix was to rebuild the commits from trees there and move only the misplaced hunks.

Also found by searching

  • middle commits of my split branch do not compile, only the final tree builds
  • I have a big uncommitted diff mixing several features and need separate commits
  • how do I split working tree changes into multiple commits that each pass tests
  • git stash create snapshot, partition hunks by feature, verify each intermediate commit
  • I have a huge uncommitted diff with several features mixed and each commit must compile
  • how do I split one working tree change into multiple buildable commits when hunks overlap?
  • how do I verify every commit in a split branch builds, not just the final tree?
  • git add -p split big change into logical commits, stash create snapshot, detached worktree build each step ninja -k 0