stack-plan
Use when you need to plan commits, restructure a stack, or commit uncommitted work as organized atomic commits. Builds a stack from a description, uncommitted work, or existing commits INSTEAD of dumping everything into one monolithic commit with git add -A && git commit, or manual git rebase -i, git reset --soft, or ad-hoc git move sequences. Prevents: wrong commit ordering, forward references, git move -F panics, untested intermediates.
- License 许可
- Unlicense
- 最近更新
- 2026-11-15
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- stack-plan
- description
- Use when you need to plan commits, restructure a stack, or commit uncommitted work as organized atomic commits. Builds a stack from a description, uncommitted work, or existing commits INSTEAD of dumping everything into one monolithic commit with git add -A && git commit, or manual git rebase -i, git reset --soft, or ad-hoc git move sequences. Prevents: wrong commit ordering, forward references, git move -F panics, untested intermediates.
- argument-hint
- <plan | range | (none for working tree)>
- compatibility
- Requires git-branchless
- disable-model-invocation
- false
02 资源文件 2
- 参考文档references/git-branchless.md37 B
- 参考文档references/philosophy.md33 B
03 SKILL.md 正文
Plan and execute a commit stack. Determines mode automatically based on input:
- From a plan/description — user describes what needs to be built; skill produces the commit ordering and executes it
- From uncommitted work — working tree has changes; skill classifies and commits them as an atomic stack
- From existing commits — range like
main..HEAD; skill restructures into a clean atomic stack
Pre-flight
-
Load references — read
references/philosophy.mdandreferences/git-branchless.md(relative to this skill's directory) before proceeding. -
Confirm repository initialization. A devenv project with
git.branchless.enablerunsgit branchless initon every shell entry. Anywhere else, check and stop if it is missing:git config --get branchless.core.mainBranch >/dev/null || echo "not initialized: ask the user to run git branchless init"Do not run
git branchless inityourself: it rewrites the repository's shared configuration and hooks for every worktree. -
Check for stale rebase state:
ls -d "$(git rev-parse --git-path rebase-merge)" \ "$(git rev-parse --git-path rebase-apply)" 2>/dev/nullIf either exists, run
git rebase --abortbefore proceeding. In a linked worktree.gitis a file, so ask Git for the paths. -
Select this worktree's own stack as
$STACK, and use it wherever this skill needs the stack or its base:main_branch="$(git config branchless.core.mainBranch || echo main)" remote="$(git config "branch.${main_branch}.remote" || echo origin)" base="$(git rev-parse --abbrev-ref "${main_branch}@{upstream}" 2>/dev/null \ || echo "${remote}/${main_branch}")" git rev-parse --verify -q "${base}^{commit}" >/dev/null || base="$main_branch" STACK="descendants(roots((stack() & ::HEAD) - ::$base)) - ::$base"$baseis main's upstream ref (the last fetched one), or localmainwhen there is none. Plainstack()is not your stack while localmainis behind its upstream, the normal state when worktrees branch fromorigin/main: it also holds the upstream commits and every stack another worktree branched from them. See Selecting your own stack inreferences/git-branchless.md.
Determine Mode
Examine $ARGUMENTS and repo state to select the mode:
- If
$ARGUMENTScontains--tip-onlyor requests tip-only redistribution → Restructure mode (tip-only redistribution) - If
$ARGUMENTScontains--rootor requests a from-root restructure → Restructure mode (from-root) - If
$ARGUMENTSlooks like a commit range (main..HEAD,hash1..hash2, branch name) → Restructure mode - If
$ARGUMENTSis a natural-language description of work to do → Plan mode - If
$ARGUMENTSis empty and the working tree has uncommitted changes → Working tree mode - If
$ARGUMENTSis empty and the working tree is clean → ask the user what they want to do
Plan Mode
The user describes work to be done. Build the stack from scratch.
-
Understand the task from
$ARGUMENTS. If the description is vague, ask clarifying questions. -
Design the commit stack following the ordering in
references/philosophy.md:- Refactoring / cleanup (separate from feature work, always first)
- Dependencies / build config changes
- Scaffolding / types / interfaces / schemas
- Core business logic
- Edge cases / error handling
- Tests
For each commit, note:
- One-sentence commit message (Conventional Commits format)
- Which files will be created or modified
- Estimated line count (target 50-200 per commit)
- Dependencies on prior commits (no forward references)
-
Verify no forward references: each commit must only reference files, imports, and config that exist in it or earlier commits. Documentation goes WITH the feature it documents. Dependencies arrive in the commit that first uses them.
-
Present the plan to the user:
Proposed stack (N commits): 1. type(scope): description — files X, Y (~80 lines) 2. type(scope): description — files A, B (~120 lines) 3. type(scope): description — files C, D, E (~150 lines) ...Wait for user approval. Adjust if they have feedback.
-
Execute the plan — implement each commit in order:
# Write the code for commit 1 git add <files> git commit -m "type(scope): description" # Write the code for commit 2 git add <files> git commit -m "type(scope): description"After each commit, verify the codebase is in a working state.
Working Tree Mode
Uncommitted changes exist. Classify and commit them as a stack.
-
Survey the changes:
git status --short git diff --stat git diffRead the full diff. Count total lines changed.
-
Classify every change into logical groups following the ordering in
references/philosophy.md. For each group, note:- Which files and hunks belong to it
- A one-sentence commit message
- Estimated line count (target 50-200 per commit)
If a file has changes spanning multiple groups, it will need
git add -pto split hunks across commits. -
Present the plan to the user (same format as Plan mode step 4). Wait for user approval.
-
Commit each group in plan order:
# For file-level grouping: git add <file1> <file2> git commit -m "type(scope): description" # For hunk-level splitting within a file: git add -p <file> git commit -m "type(scope): description"After each commit, verify nothing was missed or double-counted:
git diff --stat # remaining uncommitted changes
Restructure Mode
Existing commits need to be reorganized into a clean atomic stack.
-
Parse the range from
$ARGUMENTS. Resolve to a<base>and<tip>:# For range syntax (base..tip): BASE=<resolved base> TIP=<resolved tip> # For a branch name, find the merge-base with the upstream, not local main: BASE=$(git merge-base "$base" <branch>) TIP=<branch> # For --root (entire history): BASE=<empty tree> TIP=HEAD -
Understand the current state:
git sl git log --oneline --reverse $BASE..$TIP # or --root for from-root git log --reverse --stat --format="=== %h %s ===" $BASE..$TIP git diff --stat $BASE..$TIP # From-root (no base commit): git log --oneline --reverse --root git log --reverse --stat --format="=== %h %s ===" --root git diff --stat $(git hash-object -t tree /dev/null) HEADRead the full diff and per-commit stats. Count total lines changed.
A range written against local
main(main..HEAD) also holds the upstream commits whilemainis stale, and using the upstream tip itself as the base stages upstream-only commits as deletions. For this worktree's stack, use$(git merge-base "$base" HEAD)..HEAD. -
Classify every change into logical groups (same as Working Tree mode step 2).
-
Identify files with intermediate states: before planning the commit sequence, list every file that will have DIFFERENT content across multiple commits (e.g. README.md, CLAUDE.md, flake.nix growing incrementally). For each, write out EXACTLY what content it should have at each commit boundary. Plan ALL intermediate states BEFORE flattening — discovering them as you go leads to fixups and tree hash mismatches. This is the #1 source of rework during restructure.
-
Present the plan (same format as Plan mode step 4). Wait for user approval.
-
Check for uncommitted work before flattening:
git status --shortIf there are uncommitted changes, warn the user and ask whether to stash or commit them first.
-
Confirm the recovery point. Record the current commit ID in the task report. Do not create or delete a generic repository-global backup branch; it may belong to another worktree or session.
-
Save the tree hash for post-verification:
FINAL_TREE=$(git rev-parse HEAD^{tree}) -
Extract intermediate file states before flattening. For every file identified in step 4 as having multiple states, save each version to a temp directory using
git show. After flattening, the working tree only contains the final state — earlier versions are gone.STATES_DIR=$(mktemp -d /tmp/restructure-states.XXXXXX) # For each (file, commit) pair from step 4: git show "${HASH}":path/to/file > "${STATES_DIR}/filename.${HASH}"Include stub files (placeholder modules,
.gitkeepfiles) that scaffold commits create before later commits replace them with real content. Restore these withcportouchbefore staging each intermediate commit. -
Flatten the range into the working tree:
# Standard range (has a base commit):
git reset --soft $BASE
git restore --staged .
# From-root (no base commit):
git checkout --orphan restructure-wip
git reset
# Note: git-branchless hook will panic on orphan branches — this is
# harmless. The checkout succeeds. Suppress noise with:
# 2>&1 | grep -v "^branchless:"
Verify the working tree matches the original diff:
# Standard range (files are unstaged):
git diff --stat # should match step 2's total diff
# From-root (all files are untracked after orphan+reset):
git status --short | wc -l # should match step 2's file count
-
Commit each group in plan order (same as Working Tree mode step 4). For files with intermediate states (identified in step 4), restore the correct version from
$STATES_DIRBEFORE staging for each commit. Do not rely on the final working tree content — it represents the end state, not intermediate states.After staging each commit, check for common mistakes:
- Orphaned deletions: when staging renamed/moved files, the old paths
remain as unstaged deletions. Check
git statusand stage them in the same commit. git add -uduring multi-commit rebuild: stages ALL modified tracked files, not just the ones for this commit. Always use explicitgit add <file>paths.- Verify staged content: run
git diff --cached --statto confirm only intended files are staged.
- Orphaned deletions: when staging renamed/moved files, the old paths
remain as unstaged deletions. Check
Tip-Only Redistribution
A variant of restructure mode for branches where the commit history is noise and only the final tree state matters.
When to use:
- More than 50% of commits are pivots, experiments, or failed approaches
- The stack has been restacked 3+ times
- Failed approaches were tried and abandoned in the history
- Commit messages are mostly "fix", "WIP", "try again", or no longer match their diffs
Approach:
-
Analyze the final tree, not the history. Run
/stack-summary --rootor diff the tip against the base to understand what FILES exist at HEAD:git diff --stat $BASE..HEAD git diff $BASE..HEADIgnore the commit log entirely — don't try to preserve or reorder existing commits. The history is discarded.
-
Classify every file in the total diff into logical groups following the ordering in
references/philosophy.md. For each group, note:- Which files belong to it
- A one-sentence commit message
- Estimated line count (target 50-200 per commit)
-
Apply dependency timing audit to determine ordering. Each planned commit must only reference files, imports, and config that exist in it or earlier commits. Documentation goes WITH the feature it documents.
-
Present the plan to the user (same format as Plan mode step 4). Wait for user approval.
-
Flatten to base and recommit from scratch using Restructure mode steps 6-11 (check for uncommitted work, create backup, save tree hash, flatten, commit each group).
-
The resulting stack should tell a clean incremental story for reviewers — not the story of how development actually happened.
Key differences from standard restructure:
- Standard restructure preserves and reorders existing commits
- Tip-only redistribution ignores history and plans from the final tree
- No need to identify intermediate file states (step 4 of standard restructure) because the history is not a meaningful input — only the final diff against the base matters
- No need to extract intermediate file states (step 9 of standard restructure) — intermediate content is authored fresh from the plan
Post-execution
-
Choose the destination for a from-root rebuild. Stop and ask the user which new branch should own the orphan history. Never replace local
main; the primary checkout may hold it. -
Verify the result:
git sl git log --oneline --reverseShow the user the new stack.
-
REQUIRED: Confirm total diff is identical (Restructure mode only):
# Tree hash comparison (catches ALL content differences): test "$(git rev-parse HEAD^{tree})" = "$FINAL_TREE" && echo "trees match"Do NOT proceed if trees don't match. Fix the diverging files first using fixup commits (
git commit --fixup <hash>+ autosquash rebase). Common causes: intermediate file state written incorrectly, orphaned deletions missed, orgit add -upulling unrelated changes. -
Clean up stale artifacts: check for self-referencing symlinks or other working tree debris:
git status --short # should be cleanRemove any untracked artifacts that weren't in the original tree.
-
Run tests if a test command is identifiable:
git test run -x '<test-command>' --jobs <N> '<revset>'Pick
<revset>per Choosing the test revset inreferences/git-branchless.md, readingstack()there as$STACK. Restructuring is one of the cases that argues for$STACKeven inside a single PR — the intermediate commits are newly built and untested — but say so rather than widening silently, and size<N>by memory, not cores.Report any commits that break the build.
Tips
- Don't lose changes. After flattening, double-check
git diff --statmatches the original range diff. If anything is missing, stop and investigate. - Prefer
git add <files>overgit add -pwhen the changeset groups by file without needing hunk splitting. - Respect user-requested commit boundaries — not every restructure needs to flatten everything.
- From-root restructures flatten the entire history. Use
git checkout --orphan+git reset(notgit reset --softwhich needs a parent commit). The branchless hook will panic — ignore it. - Intermediate file states are the #1 source of rework. Files like README.md and CLAUDE.md that grow across many commits must be written with partial content at each step. Plan these states BEFORE flattening.
- For files with complex structure (nested sections, cross-references), a full rewrite at the appropriate commit is often cleaner than incremental Edit operations that can cause structural nesting errors.
- Expect hook failures during restructure. PostToolUse hooks (agnix linting,
treefmt formatting, etc.) fire when using Write/Edit during a restructure. The
working tree is intentionally inconsistent between commits — hook errors are
harmless and will pass once all files are committed. Use
--no-verifyon intermediate commits if needed. - For corrections discovered after rebuilding, stop and use
/stack-fixwith a known target. Do not improvise a rebase, move, revise, or restack repair.
04 同仓库更多 Skills
stack-test
Use when you need to run tests or formatters across commits in a stack. Use INSTEAD of manual git test run or looping git checkout + test. Prevents: testing every commit when only the tip has to pass, unbounded parallel jobs exhausting memory, cache misunderstandings.
stack-summary
Use when you need to audit, analyze, or review stack quality before restructuring. Produces a structured summary with per-commit classification, philosophy audit, and violation flags. Output feeds directly into /stack-plan restructure mode. Use INSTEAD of manual git log inspection.
stack-submit
Use when you need to push, submit, or create pull requests for a commit stack. Rebases only the current stack onto upstream main, validates, and pushes INSTEAD of manual git sync + git submit + PR/MR creation. Handles branch creation, stacked PR/MR creation with correct base branches.
stack-split
Use when you need to break up, split, or decompose a large commit into smaller atomic commits. Use INSTEAD of manual git rebase -i + edit or git reset HEAD^. Prevents: non-working intermediate commits, wrong split ordering, missed downstream restack.
stack-fix
Use when you need to fix, correct, or update content in an earlier commit — line-level edits, content moves, or structural changes. Absorbs staged changes into correct stack commits INSTEAD of running git absorb or git commit --fixup via Bash, or hand-running git prev + edit + git amend + git restack --merge. Prevents: missed dry-run preview, leftover staged changes going unnoticed, forgetting to restack. Falls back to guided manual amend when absorb cannot route hunks.
peer-communication
How to write replies a person reads: answer first, plain words, easy to scan. Load before your first reply in an interactive session.