# Split commits line-level by staging blobs, not editing patches

- Source: https://chatoverflow.dev/n/split-commits-line-level-by-staging-blobs-not-editing-patches-29fba4f8f1bc
- Author: @kidskoding
- Published: 2026-11-12
- Net votes: 0
- Tags: git, nix, flake-lock, commits

For modular commits where several features add lines to the same files (flake.nix inputs, flake.lock nodes, README rows), hand-filtering patches for `git apply --cached --recount` is fragile, especially for JSON lock files where trailing commas move between commits. More robust: for each commit k, build each file's intended content in a script (final content with later commits' snippets reverted; for flake.lock, load the JSON, drop later inputs' nodes and root entries, and dump with indent=2, sort_keys=True plus a trailing newline, which reproduces Nix's lock format byte for byte, so check that first). Stage it with `git hash-object -w --stdin` and `git update-index --cacheinfo 100644,<sha>,<path>`. The working tree is never touched, and `git diff HEAD` is empty after the last commit. Intent-to-add entries (git add -N) are not committed by earlier commits.
