Git & Version Control Standards
Git & Version Control Standards
Section titled “Git & Version Control Standards”1. Repository Structure
Section titled “1. Repository Structure”Branch Strategy
Section titled “Branch Strategy”- Main Branch:
main(ormasterfor legacy). Protected branch. All production code. - Development Branch:
develop(optional). Integration branch for features. - Feature Branches:
feature/description(e.g.,feature/user-authentication) - Bug Fixes:
fix/description(e.g.,fix/email-validation) - Hotfixes:
hotfix/description(e.g.,hotfix/security-patch) - Releases:
release/version(e.g.,release/1.2.0)
Branch Naming
Section titled “Branch Naming”- Format:
type/description(kebab-case) - Branch prefixes:
feature(or its short formfeat),fix,hotfix,release,refactor,docs,test - Description: Concise, descriptive (3-5 words)
Branch prefixes are not commit types. The words above name branches (
feature/user-auth,hotfix/security-patch). Commit messages use the Conventional Commits types below — notefeat, notfeature, and there is nohotfix/releasecommit type. The canonical commit-type set is defined once inshared/core-standards.md(§ Commit Messages); this document must stay aligned with it.
2. Commit Messages
Section titled “2. Commit Messages”Conventional Commits Format
Section titled “Conventional Commits Format”<type>(<scope>): <subject>
<body>
<footer>Commit Types
Section titled “Commit Types”The canonical cross-cutting set (from shared/core-standards.md) is:
feat, fix, refactor, test, docs, chore, perf, ci.
- feat: New feature
- fix: Bug fix
- refactor: Code refactoring (no functional changes)
- test: Adding or updating tests
- docs: Documentation changes
- chore: Maintenance tasks (dependencies, build config)
- perf: Performance improvements
- ci: CI/CD changes
Additional Conventional Commits types the linter also accepts
(scripts/lint-checks/common/conventional-commits.sh) but which fall outside
the canonical set: build (build-system changes), style (formatting-only
changes), revert (reverting a prior commit). Prefer the canonical types.
Commit Guidelines
Section titled “Commit Guidelines”- Subject: Imperative mood, 50 characters or less, no period
- Body: Explain “what” and “why”, wrap at 72 characters
- Footer: Reference issues/PRs:
Closes #123,Fixes #456 - Scope: Optional, indicates area of change (e.g.,
domain,api,ui)
Scope Allow-List
Section titled “Scope Allow-List”Scopes drift when every author invents their own (edit/macos, editor/windows
and windows/edit coexisting in one history). Projects SHOULD pin the accepted
scopes in .standards.yml:
commits: scopes: [api, domain, ui, infra]When the list is present, every scoped commit must use one of the listed
scopes; unscoped commits (fix: ...) remain allowed. The linter module, the
commit-msg hook templates and the PR-title check all read this one list.
Pull Request Titles Are Commits
Section titled “Pull Request Titles Are Commits”With squash and merge (the preferred strategy, § 4) GitHub writes the PR
title — not the branch commits — as the commit subject on the mainline. A
perfectly conventional branch squashed under a prose title (“Phase 1: repository
scaffold”) still lands a non-conventional commit in main, and a commit-msg
hook never sees it.
- PR titles MUST follow the same
<type>(<scope>): <subject>format. - The
standards-reviewcomposite action checks the title on every PR and fails the check by default (pr-title-check: warndowngrades it to an annotation,offdisables it). - Validate a candidate title locally with
.standards/scripts/check-commit-message.sh "feat(api): add endpoint".
Automated Enforcement
Section titled “Automated Enforcement”scripts/lint-checks/common/conventional-commits.sh (run by lint-standards.sh)
inspects the last 50 non-merge commits by default. Tune the range in
.standards.yml:
commits: window: 100 # inspect the last N non-merge commits since: origin/main # or: inspect <ref>..HEAD (wins over window)The module also accepts --since <ref> / --window <n> flags, and the
standards-review action defaults the range to the PR base so a PR is judged on
its own commits (that PR range beats a configured window; only since or an
explicit flag overrides it). Merge commits, fixup!/squash! prefixes and git revert
subjects are exempt.
Examples
Section titled “Examples”feat(domain): add user email validation
Implement RFC 5322 compliant email validation in Email value object.Rejects invalid formats at domain boundary to fail fast.
Closes #123fix(api): handle null response in user endpoint
Return 404 instead of 500 when user not found. Prevents servererrors from propagating to client.
Fixes #4563. Commit Best Practices
Section titled “3. Commit Best Practices”Atomic Commits
Section titled “Atomic Commits”- One Logical Change: Each commit should represent one complete, logical change
- Small Commits: Prefer multiple small commits over one large commit
- Testable: Each commit should leave the codebase in a working state
Commit Frequency
Section titled “Commit Frequency”- Regular Commits: Commit frequently (at least daily during active development)
- Logical Units: Commit when a logical unit of work is complete
- Before Break: Commit before leaving work for extended periods
What to Commit
Section titled “What to Commit”- Source Code: All source code, tests, and configuration
- Documentation: README, docs, comments
- Configuration: Build files, CI config, editor configs
- Dependencies: Lock files (package-lock.json, Cargo.lock, etc.)
What NOT to Commit
Section titled “What NOT to Commit”- Secrets: API keys, passwords, tokens, credentials
- Build Artifacts: Compiled binaries,
dist/,build/,target/ - Dependencies:
node_modules/,venv/,.envfiles - IDE Files:
.idea/,.vscode/(unless project-specific settings) - OS Files:
.DS_Store,Thumbs.db
4. Git Workflow
Section titled “4. Git Workflow”Feature Development
Section titled “Feature Development”- Create feature branch from
mainordevelop - Make atomic commits with descriptive messages
- Push branch regularly (at least daily)
- Open Pull Request when feature is complete
- Address review feedback with additional commits
- Squash commits if requested during review
- Merge via Pull Request (no direct pushes to main)
Pull Request Process
Section titled “Pull Request Process”- Title: Follow conventional commit format
- Description: Explain what, why, and how. Include screenshots for UI changes
- Size: Keep PRs focused and reviewable (< 400 lines when possible). When a task cannot fit, stack it (§ 11) instead of growing one PR
- Tests: Include tests for new features and bug fixes
- Documentation: Update documentation for user-facing changes
- CI: All CI checks must pass before merge
Code Review
Section titled “Code Review”- Required: At least one approval before merge
- Response Time: Review within 24-48 hours
- Feedback: Be constructive and specific
- Approval: Approve only when code meets standards
Merge Strategy
Section titled “Merge Strategy”- Squash and Merge: Preferred for feature branches (clean history)
- Merge Commit: Use for important features (preserve branch context)
- Rebase: Use for keeping feature branches up to date (avoid merge commits)
5. Tagging and Releases
Section titled “5. Tagging and Releases”Semantic Versioning
Section titled “Semantic Versioning”Format: MAJOR.MINOR.PATCH (e.g., 1.2.3)
- MAJOR: Breaking changes
- MINOR: New features (backward compatible)
- PATCH: Bug fixes (backward compatible)
Tagging
Section titled “Tagging”- Format:
v1.2.3(prefixed withv) - Annotated Tags: Use annotated tags for releases:
git tag -a v1.2.3 -m "Release 1.2.3" - Release Notes: Include release notes in tag message or GitHub release
- Every released changelog heading is a tag. A
CHANGELOG.mdthat lists 0.1.4 and 0.1.5 while onlyv0.1.2/v0.1.3exist — andpackage.jsonstill says 0.1.3 — is a release process that stopped at the prose: nothing is reproducible from the “released” versions. The tag step is part of the release job; a release workflow that publishes an artifact without runninggit tagis the same defect from the other side. - Enforcement:
scripts/lint-checks/common/changelog-tags.sh(inmake lint-standardsand the standards-review action) reads every released## [x.y.z]heading and fails when one has nov<version>(or<version>) tag — except the single topmost (newest) heading while every other one is tagged, which is a release in flight (the release PR adds the heading, the tag follows the merge) and warns instead; it warns when the rootpackage.json/Cargo.toml/pyproject.tomlversion is not the latest release, and when a shallow CI clone cannot see a heading’s tag (usefetch-depth: 0orfetch-tags: true; a--depthclone only carries the tags inside the fetched history, so that case is unverifiable rather than a failure — unless the tag namespace is complete, which the check confirms against the remote). Declarerelease.changelogandrelease.tag_prefixin.standards.ymlwhen the file or the prefix differ.
Release Process
Section titled “Release Process”- Update version in code and
CHANGELOG.md - Create release branch:
release/v1.2.3 - Final testing and bug fixes
- Merge to
mainand tag:git tag -a v1.2.3 - Push tag:
git push origin v1.2.3 - Create GitHub/GitLab release with notes
- Merge back to
developif applicable
Release Version vs. Build Version
Section titled “Release Version vs. Build Version”For products that deploy continuously, distinguish two version numbers:
- Release version — manual semantic version (
MAJOR.MINOR.PATCH), bumped deliberately at a release. It is what users see and what gates a coordinated release (e.g. apps and marketing site move together on a release-version bump). - Build version — derived automatically and ticking on every commit to the
mainline (e.g.
git rev-list --count HEAD). Useful for “exact build” reporting and crash triage, never for user-facing semver decisions.
Surface both in an About/diagnostics view. A value only tree-shakes in if it is actually rendered — exporting it isn’t enough.
Deploy Channels & Promotion
Section titled “Deploy Channels & Promotion”- Separate staging/preview from production by channel. The mainline branch auto-deploys to the staging channel; production is gated on an explicit promotion. Never deploy production off arbitrary feature branches.
- If the deploy platform’s branch control takes no wildcard, gate production on a
single long-lived
releasebranch (notrelease/*) and promote by fast-forwarding mainline into it (git push origin main:release). - Channel-gate not-yet-released content (e.g. an “unreleased” changelog section
visible only on the staging channel) via a build-time channel variable. Verify
the variable name matches what the deploy platform actually injects — a typo
(
PUBLIC_CHANNELvsVITE_CHANNEL) silently ships the wrong content. - A green release does not prove production updated. End the release with an
automated version assertion against the live deployment (fetch it, assert the
new release/build number), not “the promote step succeeded”. The deploy
platform’s credentials and branch protections live outside the repo and fail
invisibly — a rolled or deleted build token, or a force-push-protected
releasebranch that silently rejects the fast-forward promote — so a green in-repo pipeline is necessary but not sufficient. See arch-08_ci_cd_pipeline_standards.md §7 for the CI-side smoke check.
Scripted, Reviewable Release Cut
Section titled “Scripted, Reviewable Release Cut”Automate the release cut as a script that produces a reviewable PR, not a manual sequence of edits:
- A pure script performs the semver bump and promotes changelog entries
(
unreleased/→ the released version section), and emits the release-notes body. - The script opens (or prepares) a PR for human review before anything is tagged or promoted. Tagging and channel promotion happen only after that PR merges.
- Mind CI-identity loop-prevention: a PR opened by a CI token often does not trigger the normal PR gate, so a fully hands-free release usually needs a dedicated app token or a human to open/merge the cut PR. Document which model the repo uses.
See arch-08_ci_cd_pipeline_standards.md §7 for the CI side of channel promotion.
6. Git Configuration
Section titled “6. Git Configuration”Required Settings
Section titled “Required Settings”# User identificationgit config --global user.name "Your Name"
# Default branch namegit config --global init.defaultBranch main
# Push behaviorgit config --global push.default simplegit config --global push.autoSetupRemote true
# Pull behaviorgit config --global pull.rebase false # Use merge by defaultRecommended Settings
Section titled “Recommended Settings”# Color outputgit config --global color.ui auto
# Editorgit config --global core.editor "code --wait" # VS CodeGit Aliases
Section titled “Git Aliases”See Git Aliases Reference for the full alias catalog. Run scripts/setup-git-aliases.sh to install.
7. Git Hooks
Section titled “7. Git Hooks”Pre-commit Hook
Section titled “Pre-commit Hook”- Linting: Run linters (ESLint, Pylint, Clippy)
- Formatting: Auto-format code (Prettier, Black, rustfmt)
- Tests: Run fast unit tests
- Validation: Check commit message format
Pre-push Hook
Section titled “Pre-push Hook”- Tests: Run full test suite
- Type Checking: Run type checkers (TypeScript, mypy)
- Build: Verify project builds successfully
Commit-msg Hook
Section titled “Commit-msg Hook”- Format Validation: Ensure commit messages follow conventional format
- Length Check: Validate subject line length
Implementation
Section titled “Implementation”Use tools like:
- Husky (Node.js)
- lefthook (any stack; single binary)
- pre-commit (Python)
- git-hooks (Rust)
- Custom shell scripts
Conventional Commits Hook Templates
Section titled “Conventional Commits Hook Templates”The standards repo ships ready-made commit-msg hooks that call
scripts/check-commit-message.sh (format + commits.scopes allow-list).
setup.sh prints the install commands but never installs a hook manager for
you:
# husky (v9+)npx husky init # once, if .husky/ does not exist yetcp .standards/templates/husky-commit-msg.example .husky/commit-msgchmod +x .husky/commit-msg
# lefthookcp .standards/templates/lefthook.yml.example lefthook.yml # or merge the blocks into yourslefthook installmake doctor recognises lefthook.yml and .husky/ as hook managers and
warns when neither defines a commit-msg hook. Because both managers take over
core.hooksPath, the native .git/hooks/post-merge reminder that setup.sh
writes stops running; the lefthook template restores it with a post-merge
block and doctor warns when a husky project lacks one.
Hooks guard branch commits only — pair them with the PR-title check (§ 2, “Pull Request Titles Are Commits”) to protect squash-merged history.
8. .gitignore Patterns
Section titled “8. .gitignore Patterns”Language-Specific
Section titled “Language-Specific”Python:
__pycache__/*.py[cod]*$py.class*.so.Pythonvenv/env/.venvNode.js:
node_modules/npm-debug.log*yarn-debug.log*yarn-error.log*.pnpm-debug.log*dist/build/Rust:
target/Cargo.lock # For libraries, not applicationsJava:
*.class*.jar*.war*.ear.gradle/build/Test / coverage output
Section titled “Test / coverage output”Ignore generated coverage reports so they are never committed:
coverage/Common tools that write here: Vitest, Jest, pytest, Istanbul/c8, lcov. Projects using the standards setup or install scripts get this pattern added automatically.
General
Section titled “General”# Environment.env.env.local.env.*.local
# IDE.idea/.vscode/*.swp*.swo*~
# OS.DS_StoreThumbs.db
# Logs*.loglogs/9. Large Files and Git LFS
Section titled “9. Large Files and Git LFS”Git LFS
Section titled “Git LFS”Use Git LFS for:
- Binary files > 100MB
- Media files (images, videos)
- Compiled binaries
- Database dumps
Configuration
Section titled “Configuration”git lfs installgit lfs track "*.psd"git lfs track "*.zip"10. Branch Lifecycle
Section titled “10. Branch Lifecycle”Cleanup After Merge
Section titled “Cleanup After Merge”Branches MUST be deleted — both local and remote — immediately after their PR is merged or closed. Do not accumulate stale branches.
- PR merge: Enable “Automatically delete head branches” in GitHub repo settings. This handles the remote branch. Delete the local branch manually after merge.
- Squash merges: Squash-merged branches are not detected by
git branch --merged. Usegit branch -d <branch>(lowercase-d) which checks if the branch is fully merged; if it refuses, verify the work landed via PR, then usegit branch -D <branch>. - Worktrees: Remove the worktree before deleting its branch:
git worktree remove <path>thengit branch -D <branch>.
Naming Branches for Traceability
Section titled “Naming Branches for Traceability”Include enough context in the branch name to identify the work without checking the log:
feat/preview-scroll-optimization ✓ clear purposefix/sidebar-drag-crash ✓ clear purposeworktree-agent-a0939472 ✗ opaque, impossible to triage latertmp ✗ no contextcopilot/sub-pr-7 ✗ meaningless without the PRAgent-generated branches MUST follow the same type/description convention as human branches. Opaque IDs or numeric suffixes alone are not acceptable names.
Periodic Audit
Section titled “Periodic Audit”Run a branch audit at least every two weeks (or before starting a new feature):
# List local branches not on main, sorted by last commit dategit branch --no-merged main --format='%(committerdate:short) %(refname:short)' | sort
# List remote branches with no recent activity (>14 days)git for-each-ref --sort=committerdate --format='%(committerdate:short) %(refname:short)' refs/remotes/origin | head -20
# Delete local branches whose remote is gonegit fetch --prunegit branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -DProjects using make SHOULD include a cleanup target:
branch-cleanup: ## Delete local branches whose remote tracking branch is gone @git fetch --prune @git branch -vv | grep ': gone]' | awk '{print $$1}' | xargs -r git branch -D @echo "Remaining branches:" && git branchBranch Limits
Section titled “Branch Limits”As a guideline, a repository should have fewer than 10 active branches at any time. If you have more, audit and clean up before creating new ones. This applies to both local and remote branches (excluding main).
Garbage Collection
Section titled “Garbage Collection”- Run
git gcperiodically (usually automatic). - After large cleanups, run
git gc --prune=nowto reclaim space immediately.
Security
Section titled “Security”- Secrets Scanning: Use tools like
git-secrets,truffleHog - History Rewriting: Avoid force-pushing to shared branches
- Access Control: Use branch protection rules (GitHub/GitLab)
Backup
Section titled “Backup”- Remote Repository: Always push to remote (GitHub, GitLab, etc.)
- Multiple Remotes: Consider backup remote for critical projects
- Regular Pushes: Push at least daily during active development
11. Stacked PRs (gh-stack)
Section titled “11. Stacked PRs (gh-stack)”The default unit of work is one task = one branch = one PR. When a task is too
large to review as a single PR, the unit becomes one task = one stack of
focused PRs: an ordered chain of branches in which each layer targets the one
below it and the bottom targets the trunk (main). GitHub’s stacked pull
requests — driven locally by the official
gh stack
CLI extension — are the preferred way to do this. Each PR shows only its own
layer’s diff, the layers are linked as a stack in the PR UI, and GitHub rebases
and retargets the remaining layers as the bottom merges.
When to stack
Section titled “When to stack”Stack when any of these holds — and split a single PR into a stack rather than growing it:
- Size. The diff would exceed the PR-size smell threshold in § 4 (~400 changed lines, not counting generated files and lockfiles), or it touches more than ~15 files a reviewer has to read rather than skim.
- Dependency depth. The change is an ordered chain (schema → domain → API → UI; standard → linter → docs → agent config) whose lower layers are reviewable on their own.
- Separable review. Different reviewers or CODEOWNERS own different parts, or a risky part (a migration, a security-sensitive path) deserves its own scrutiny instead of hiding inside a feature diff.
- Test governance. One small part would pull disproportionate slow lanes from the routing table (proc-05 § 5); isolating it in its own layer keeps the PR evidence (proc-05 § 8) attributable to the code it covers.
Do not stack when:
- The change fits one focused PR under the threshold — a stack of one is a PR.
- The layers would not be independently meaningful. A layer that only compiles once the next one lands is not a layer.
- The parts are trivially coupled and must land together to keep
maingreen. Stacks merge layer by layer, and every layer must leavemainworking.
Decide to stack at planning time (proc-01 § 11, proc-04 § 8), not after an oversized PR is open — though splitting an open oversized PR into a stack is allowed, and preferred over “review it in sections”.
Layer rules
Section titled “Layer rules”Every layer is a complete PR in its own right:
- Stands alone. With the layers below it as its base, the layer builds, its tests pass and coverage holds (≥ 95 % in modified modules, proc-03 § 2). No “will fix in the next layer”.
- One concern per layer. A layer is one architectural slice or one logical step, and its tests ride in the same layer as the code they cover. A refactor that enables the feature is its own lower layer (proc-03 § 9, Refactoring PRs).
- Named for traceability.
<type>/<task>/<NN>-<layer>, e.g.feat/user-export/01-schema,feat/user-export/02-api,feat/user-export/03-ui. The shared<task>segment groups the stack ingit branchoutput and the two-digit prefix sorts layers in order. Thetype/descriptionrule from § 1 and § 10 still applies — no opaque IDs. - Conventional title per layer. Each PR title is its own
<type>(<scope>): <subject>: under squash-and-merge it becomes that layer’s commit subject onmain(§ 2, “Pull Request Titles Are Commits”). A non-interactivegh stack submit --autogenerates titles — rewrite every one before the PR leaves draft (see “Create a stack”). - Stack position in the body. Every layer’s PR body states its position and
the chain:
Stack: 2 of 3 — #41 → #42 (this PR) → #43. The top layer carriesCloses #N— it lands last, so the issue closes when the whole stack has merged; every layer below carriesPart of #N(a closing keyword on the bottom would close the issue after the first layer). One close-keyword per line (§ 12).
Install
Section titled “Install”gh extension install github/gh-stack # needs gh >= 2.0 and git >= 2.36gh extension upgrade stack # keep it currentgh skill install github/gh-stack # optional: the gh-stack agent skillgh stack is the local workflow; the stack itself is a GitHub object.
Contributors without the extension can still review, approve and merge layers
in the web UI, and hand-chained PRs can be linked into a stack with
gh stack link <branch-or-pr>... or from the GitHub website.
Create a stack
Section titled “Create a stack”Work from one worktree for the whole stack (proc-04 § 8):
git fetch origingit worktree add .claude/worktrees/feat-user-export -b feat/user-export/01-schema origin/maincd .claude/worktrees/feat-user-exportgh stack init feat/user-export/01-schema # trunk = default branch; --base <branch> overrides# ... commits for the schema layer ...gh stack add feat/user-export/02-api # new branch on top of the current top# ... commits for the API layer ...gh stack add feat/user-export/03-ui# ... commits for the UI layer ...gh stack view # order, PR links, last commit per layergh stack submit # push every branch, one PR per layer, linked as a stackStart the bottom branch from the trunk explicitly (origin/main above): without
a commit-ish, git worktree add -b branches from the main checkout’s current
HEAD, and gh stack init adopts whatever that branch already carries.
In a terminal, submit opens an editor for every title and body — write the
Conventional Commit title and the stack-position line there. Each PR’s base is
the branch below it, so reviewers see only that layer. gh stack push pushes
the branches without touching PRs; gh stack sync fetches, fast-forwards the
trunk, cascades the rebase, pushes with --force-with-lease and refreshes PR
state in one step.
Non-interactive sessions (agents, CI) get no editor: submit falls back to
generated titles and opens the PRs as drafts, which never enter the review
loop. Use the deterministic flow instead — submit, rewrite, then mark ready:
gh stack submit --auto # push + draft PRs with generated titlesgh stack view --short # PR numbers per layergh pr edit 41 --title "feat(db): add export table" --body-file layer-01.mdgh pr edit 42 --title "feat(api): add /exports endpoint" --body-file layer-02.mdgh pr edit 43 --title "feat(ui): add export download view" --body-file layer-03.mdgh pr ready 41 && gh pr ready 42 && gh pr ready 43 # now reviewers and automation fireEach layer-NN.md is the per-layer body
(templates/stacked-pr-description.md.example): the stack-position line, the
issue reference and the agent attribution.
Update a stack
Section titled “Update a stack”Fix a layer in the layer that owns the change, then cascade:
gh stack checkout feat/user-export/01-schema # or: gh stack down / up / bottom / top# ... fix, commit ...gh stack rebase # fetch trunk, replay every layer above onto the fixgh stack top && make ci # re-gate the rebased tree (every affected layer, at least the top)gh stack push # --force-with-lease per branch- After
mainmoves,gh stack rebase(orgh stack sync) rebases the bottom onto the new trunk and cascades. GitHub shows a Rebase stack button when a stack goes non-linear; either path works. GitHub keeps approvals on a layer whose diff did not change across the rebase, even with “dismiss stale approvals” on. - Re-run the gate after a cascade, before the push. A lower-layer fix changes
the effective tree of every layer above it even when their own diffs are
unchanged; a rebased layer pushed ungated can break only in remote CI. Gate
every layer the rebase touched — at minimum the top, which carries the whole
tree — then
gh stack push. - A conflict pauses the rebase in the conflicted layer: resolve,
git add, thengh stack rebase --continue(or--abortto restore every branch).initenablesgit rerere, so a resolution replays on the next cascade. - Restructure — reorder, fold, insert, rename or drop layers — with
gh stack modify, thengh stack submitto re-sync the PRs. Do it before review starts, not under a reviewer. - Once a layer has a reviewer, append, don’t rewrite (proc-03 § 9, proc-04 § 8): add commits; the cascade rebase is the only history rewrite the reviewer should see.
Merge a stack
Section titled “Merge a stack”Stacks merge bottom-up, and every layer must satisfy the trunk’s protection
rules — required checks, required reviews, CODEOWNERS — as if it targeted main
directly:
gh stack merge --squash --yes 41 # merge the bottom layer (PR #41) alonegh stack merge --squash --yes 42 # merge every unmerged layer up to and including PR #42gh stack sync # then: re-base local branches on the server-side rebase- Squash per layer. Squash creates one commit per layer on
main, titled from that layer’s PR title, so a stack of three lands as three Conventional Commits in order. Merge commit and rebase are also supported; squash is the default here (§ 4). - Always name the highest reviewed layer.
gh stack mergewith no target takes the whole current stack in a non-interactive terminal (and in a terminal, the wizard offers it). Pass the PR number of the highest layer that has completed its review loop, with--yes, so unreviewed layers are never swept in — and so one unmergeable upper layer cannot fail the whole all-or-nothing operation. - Contiguous from the bottom. Merge the bottom layer alone, or any contiguous group up from the bottom, as one all-or-nothing operation. A mid-stack layer cannot merge without the layers below it.
- GitHub retargets the rest. After a merge the next unmerged layer is rebased
to target the trunk and becomes the new bottom — no manual
gh pr edit --base. The base-deletion trap in § 12 applies to dependent PRs that are not linked into a stack. - Merge queues take the whole stack in order — and the queue chooses the
merge method:
gh stack mergeignores--squashwhen the base has a queue. Configure the queue for squash, or the one-commit-per-layer and title-as-subject guarantees above do not hold. Auto-merge is not available for stacked PRs — merge explicitly once the bottom (or a contiguous group) is green and reviewed. - Sync after every partial merge. The server rebases and retargets the
remaining layers; the branches and stack metadata in your worktree do not
follow on their own. Run
gh stack syncin the stack’s worktree before touching the next layer, or its next edit sits on stale commits and the push fails its lease. When the whole stack has merged,gh stack sync --prunedeletes the merged local branches; then remove the worktree as in § 10.
Review and CI per layer
Section titled “Review and CI per layer”- Every layer runs the full PR gate. Workflows on
pull_requestrun for each PR as if it targeted the stack base, so a three-layer stack runs the gate three times, and required checks, reviews and CODEOWNERS are evaluated against the trunk for every layer. - Gate expensive lanes by stack position.
github.event.pull_request.stack(present only on stacked PRs) exposesnumber,size,position(1 = bottom),base.refandbase.sha. Keep the fast tier (proc-05 § 4) on every layer; run slow lanes only on the top layer (stack.position == stack.size, the full change) or the lowest unmerged layer (stack.base.ref == pull_request.base.ref, the one about to land). - Each layer’s PR body carries its own evidence (proc-05 § 8): the tested commit and the lanes that ran for that layer.
- Review is per layer, bottom-up — reviewer rules in proc-03 § 9, “Reviewing a Stack”.
Worked example
Section titled “Worked example”Adding a language standard to this repository is one task spanning four concerns. As a stack:
| Layer | Branch | PR title | Stands alone because |
|---|---|---|---|
| 1 | feat/lang-14-nim/01-standard |
feat(standards): add lang-14 Nim standards |
A new document; nothing consumes it yet |
| 2 | feat/lang-14-nim/02-linter |
feat(lint): add Nim checks to lint-standards |
The checks only fire when Nim files exist |
| 3 | feat/lang-14-nim/03-docs |
docs(website): publish the lang-14 Nim page |
The site builds with or without layer 4 |
| 4 | feat/lang-14-nim/04-agent-configs |
feat(agents): regenerate standards lists for lang-14 |
Generated from layer 1; make check-agent-configs green |
Each layer merges green in order, and a reviewer who only owns the linter reads layer 2.
Without the extension, or without GitHub stacks
Section titled “Without the extension, or without GitHub stacks”GitHub stacks need every branch in one github.com repository: a GitLab remote, a GitHub Enterprise Server release without the feature, or a contribution from a fork cannot use them, and a contributor may simply not have the extension. The stacked-PR rules above still apply — layer size, one concern per layer, each layer green on its own, bottom-up merge, Conventional title per layer; only the tooling changes. Hand-chain the PRs:
- Branch each layer from the one below it (bottom from the trunk) with the same
<type>/<task>/<NN>-<layer>names, and open each PR against the branch below it — the host shows only that layer’s diff. - Write the
Stack: n of mline and the layer table in every body yourself (templates/stacked-pr-description.md.example). - Propagate a lower-layer fix by rebasing each upper branch onto its parent in
order (
git rebase <parent>), re-gate, thengit push --force-with-lease. - Merge bottom-up. After each squash merge the merged layer’s commits are not
ancestors of the trunk, so transplant only the next layer’s own commits:
save the merged branch’s tip before merging, fetch the updated trunk with
git fetch origin, then rungit rebase --onto origin/main <merged-layer-tip>on the next layer (a plaingit rebase origin/mainwould replay the merged commits and conflict). Re-run the full local gate and push with--force-with-lease. Then retarget its PR to the trunk (gh pr edit <n> --base main, or the host’s equivalent) and only then delete the merged branch — § 12 explains why the order matters.
On github.com, hand-chained PRs can later be linked into a real stack from the
website or with gh stack link <branch-or-pr>..., which restores the automatic
retargeting.
12. Dependent PRs and Platform Automation Traps
Section titled “12. Dependent PRs and Platform Automation Traps”When PRs depend on each other without being linked into a stack, or are driven by automation, a handful of platform behaviors silently cause lost work or false signals. Encode them.
Hand-chained dependent PRs — the base-deletion trap
Section titled “Hand-chained dependent PRs — the base-deletion trap”When PR B is based on PR A’s branch (B’s base = A’s feature branch) and the two are not linked as a GitHub stack (§ 11):
- Merging A and deleting its branch (
gh pr merge A --squash --delete-branch) can close B. GitHub documents an automatic base update for open PRs whose base was a merged PR’s head branch, but it is not guaranteed on every deletion path (a branch removed bygit push --delete, a cleanup job or a mirror, or removed before the merge registered), and dependents have been closed this way in practice. A PR closed because its base vanished cannot be reopened, so treat the retarget below as the rule and the automatic update as a bonus. - Avoid it: before merging A, either retarget B to mainline first
(
gh pr edit B --base main) while A’s branch still exists, or merge A without deleting its branch until B is retargeted. - Recover (if B was already closed by an A merge): in B’s worktree,
git rebase --onto origin/main <A-tip-commit>to drop A’s now-redundant commits (their content is in mainline via the squash) and replay only B’s own commits, then open a fresh PR B′ → mainline referencing the closed one.
A conflicting PR runs no checks
Section titled “A conflicting PR runs no checks”A PR with merge conflicts has no merge ref, so the host runs none of its merge-ref-triggered workflows — it looks like CI never fired (zero runs created, not even queued). If a PR’s checks are entirely absent, check its mergeable state and resolve the conflict; the checks then trigger normally. Distinguish this from a self-hosted runner being offline (which produces a queued run that waits).
Other host-automation traps
Section titled “Other host-automation traps”- One close-keyword per line.
Closes #A, #B, #Conly auto-closes the first issue. Write oneCloses #Xper line to fan out across issues. - Check polling can settle early. Right after a push, only fast external checks are registered; host-run jobs appear seconds later. A poll that exits as soon as “all known checks are non-pending” can exit before real CI starts — guard with a minimum-expected-check count.
gh pr createinfers the branch from the current directory. Run it from the correct worktree, or pass--head <branch>explicitly, or it opens a PR for whatever branch the cwd is on. See proc-04_agent_workflow_standards.md § Worktree Hygiene.