gh-stack
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesegh-stack
gh-stack
gh stackgh stack(main) <- auth <- api <- frontendLeft is the bottom, right is the top. is based on and merges first;
merges last. moves toward the top, away from trunk; moves toward it.
Foundational work belongs at the bottom, code that depends on it above. For how to choose the
layers, read .
authmainfrontendupdownreferences/stack-design.mdgh stackgh stack(main) <- auth <- api <- frontend左侧是底部,右侧是顶部。 基于 创建,会最先合并; 最后合并。 指令朝向顶部移动,远离主干; 指令朝向主干移动。基础工作应放在底部,依赖它的代码放在上方。关于如何选择层级,请阅读 。
authmainfrontendupdownreferences/stack-design.mdSetup
安装配置
bash
gh extension install github/gh-stack
git config rerere.enabled true # remember conflict resolutions
git config remote.pushDefault origin # required if the repo has more than one remotebash
gh extension install github/gh-stack
git config rerere.enabled true # 记住冲突解决方案
git config remote.pushDefault origin # 若仓库有多个远程仓库则需配置此项Non-interactive use
非交互式使用
gh stackMultiple remotes: never run , , , , or without
unless is configured. and have no
flag and require the config.
pushsubmitsyncrebaselink--remote <name>remote.pushDefaultcheckouttrunk--remote| Always run | Never run bare | Why |
|---|---|---|
| | opens a TUI under a PTY |
| | prompts for a title per new PR |
| | |
| | prompts for branch names |
| | prompts for a name, and fails even when piped |
| | opens a selection menu |
| | |
| (none) | | TUI-only, no non-interactive path |
- is safe in both modes, but it is formatted for humans. Use
view --shortto parse.--json - when a different local stack already covers those branches cannot be forced. Run
checkout <pr>first (this keeps the stack on GitHub), then retry.gh stack unstack --local
gh stack多远程仓库场景: 除非配置了 ,否则执行 、、、 或 时必须加上 参数。 和 没有 标志,需要提前配置好上述参数。
remote.pushDefaultpushsubmitsyncrebaselink--remote <name>checkouttrunk--remote| 始终运行 | 切勿裸运行 | 原因 |
|---|---|---|
| | 在PTY环境下会打开TUI |
| | 会为每个新PR提示输入标题 |
| | |
| | 会提示输入分支名称 |
| | 会提示输入名称,即使通过管道执行也会失败 |
| | 会打开选择菜单 |
| | |
| 无 | | 仅支持TUI交互,无非交互执行路径 |
- 在两种模式下都可安全使用,但它是为人类阅读设计的格式。如需解析,请使用
view --short参数。--json - 当本地已有其他堆叠覆盖目标分支时,无法强制执行。请先运行
checkout <pr>(此操作会保留GitHub上的堆叠),然后重试。gh stack unstack --local
Branch placement
分支放置原则
- Starting multi-part work: create the stack before writing files. Do not implement every concern on trunk and split it later. Put one dependent concern in each layer, bottom to top.
- Editing an existing stack: check out the layer that owns the change before editing. Never
commit a lower layer's concern on the current top branch. Run ; if ownership is unclear, inspect
gh stack view --json. Then check out the owner, edit, commit, rebase upstack, and return to top.git log --all -- <path>
bash
gh stack down # or: gh stack checkout api
git add ... && git commit -m "Add get-user endpoint"
gh stack rebase --upstack # replay every branch above onto the change
gh stack top # return to where you were
gh stack push- 启动多部分工作: 在编写代码前创建堆叠。不要在主干上实现所有需求后再拆分。将每个依赖项放在单独的层级中,从下到上排列。
- 编辑现有堆叠: 在编辑前检出负责该变更的层级。绝不要在当前顶部分支提交属于下层的变更。运行 ;若归属关系不清晰,请检查
gh stack view --json。然后检出归属分支,进行编辑、提交、向上rebase堆叠,最后返回顶部分支。git log --all -- <path>
bash
gh stack down # 或:gh stack checkout api
git add ... && git commit -m "Add get-user endpoint"
gh stack rebase --upstack # 将上方所有分支重放至变更之上
gh stack top # 返回之前所在的分支
gh stack pushCore loop
核心工作流
bash
gh stack init auth # create the stack and check out its branch
git add ... && git commit -m "Add auth middleware"
gh stack add api # next layer, branched from the current one
git add ... && git commit -m "Add API routes"
gh stack submit --auto # push every branch and open draft PRs
gh stack view --json # confirmAdd to to create PRs ready for review instead of drafts. Branch names are
verbatim: creates .
--opensubmitgh stack add refactor/foorefactor/foobash
gh stack init auth # 创建堆叠并检出其分支
git add ... && git commit -m "Add auth middleware"
gh stack add api # 创建下一层级,基于当前分支
git add ... && git commit -m "Add API routes"
gh stack submit --auto # 推送所有分支并打开草稿PR
gh stack view --json # 确认状态在 中添加 参数可创建可供评审的PR,而非草稿。分支名称会原样使用: 会创建 分支。
submit--opengh stack add refactor/foorefactor/fooStaying in sync
保持同步
bash
gh stack sync # fetch, reconcile with GitHub, rebase, push, refresh PR state
gh stack sync --prune # also delete local branches for merged PRsPruning never happens without when non-interactive. If the local and remote stacks have
diverged, prints both chains, makes no changes, and exits 0 with ; see
.
--prunesyncSync abortedreferences/troubleshooting.mdbash
gh stack sync # 拉取代码、与GitHub协调、rebase、推送、刷新PR状态
gh stack sync --prune # 同时删除已合并PR对应的本地分支非交互模式下,不加 参数不会执行清理操作。若本地与远程堆叠出现分歧, 会打印两条分支链,不做任何修改,然后以状态码0退出并提示 ;请查看 。
--prunesyncSync abortedreferences/troubleshooting.mdMerging
合并操作
Scope the merge with an argument:
bash
gh stack merge 42 --yes # PR #42 plus every unmerged PR below it
gh stack merge 7 --yes # every unmerged PR in stack #7
gh stack merge 42 --yes --squash # or --merge, --rebase, --merge-method <method>Pass a PR number to merge that PR and every unmerged PR below it, or a stack number to merge every
unmerged PR in that stack. The operation is all-or-nothing: if any PR in that set cannot merge,
none do.
Without a method flag the last-used method is reused. If the base branch uses a merge queue, the
stack is queued instead and the queue picks the method, ignoring any flag you passed with a
warning; queued PRs may land in separate groups.
通过参数指定合并范围:
bash
gh stack merge 42 --yes # 合并PR #42及其下方所有未合并的PR
gh stack merge 7 --yes # 合并堆叠#7中所有未合并的PR
gh stack merge 42 --yes --squash # 或使用 --merge、--rebase、--merge-method <method>传入PR编号会合并该PR及其下方所有未合并的PR;传入堆叠编号会合并该堆叠中所有未合并的PR。此操作是原子性的:若集合中任意PR无法合并,则所有PR都不会合并。
若未指定合并方法,会使用上次使用的方法。若基础分支使用合并队列,则堆叠会被加入队列,由队列选择合并方法,此时会忽略你传入的方法参数并给出警告;进入队列的PR可能会被分成多个组合并。
Reading state
状态读取
gh stack view --jsontrunk string
currentBranch string
branches[] name, head, base, isCurrent, isMerged, isQueued, needsRebase
branches[].pr number, url, state ("OPEN" | "MERGED" | "QUEUED"); absent when no PR existsbaseneedsRebasegh stack view --jsontrunk string
currentBranch string
branches[] name, head, base, isCurrent, isMerged, isQueued, needsRebase
branches[].pr number, url, state ("OPEN" | "MERGED" | "QUEUED"); 无PR时不会返回此字段baseneedsRebaseExit codes
退出码
| Code | Meaning | Recovery |
|---|---|---|
| 0 | Success | (none) |
| 1 | Generic error | Read stderr |
| 2 | Not in a stack | |
| 3 | Rebase conflict | Follow the Exit 3 recovery below |
| 4 | GitHub API failure | Check |
| 5 | Invalid arguments | Fix the invocation; see |
| 6 | Disambiguation required | Branch is in several stacks; check out a non-shared branch |
| 7 | Rebase already in progress | |
| 8 | Stack file locked | Another |
| 9 | Stacked PRs unavailable | Not enabled on the repository; tell the user |
| 10 | Modify recovery required | |
Exit 3 recovery:
- After : resolve the files, run
gh stack rebase, thengit add; usegh stack rebase --continueto restore the stack.gh stack rebase --abort - After : the stack has already been restored. Run
gh stack syncto recreate the conflict, then resolve and continue as above.gh stack rebase
| 代码 | 含义 | 恢复方法 |
|---|---|---|
| 0 | 执行成功 | 无 |
| 1 | 通用错误 | 查看stderr内容 |
| 2 | 未处于堆叠中 | 执行 |
| 3 | Rebase冲突 | 按照下方退出码3的恢复步骤操作 |
| 4 | GitHub API调用失败 | 检查 |
| 5 | 参数无效 | 修改命令参数;查看 |
| 6 | 需要消除歧义 | 分支存在于多个堆叠中;检出一个非共享分支 |
| 7 | Rebase已在进行中 | 执行 |
| 8 | 堆叠文件被锁定 | 另一个 |
| 9 | 堆叠PR不可用 | 仓库未启用该功能;告知用户 |
| 10 | 需要恢复Modify操作 | 执行 |
退出码3的恢复步骤:
- 执行 后:解决文件冲突,运行
gh stack rebase,然后执行git add;若需恢复堆叠,执行gh stack rebase --continue。gh stack rebase --abort - 执行 后:堆叠已恢复。执行
gh stack sync重新触发冲突,然后按照上述步骤解决并继续。gh stack rebase
Constraints
限制条件
- Stacks are strictly linear: one parent, at most one child. Use separate stacks for parallel work.
- There is no non-interactive reorder or removal. Errors may suggest , but it is TUI-only; restructure with
gh stack modifythenunstackinstead.init - PR titles and bodies are auto-generated. Use afterwards to change them.
gh pr edit - resolves against local stacks only. Use a stack or PR number to pull a stack down from GitHub.
checkout <branch-name>
- 堆叠是严格线性的:每个分支只有一个父分支,最多一个子分支。并行工作请使用独立堆叠。
- 不支持非交互式的重排序或移除操作。错误信息可能会建议使用 ,但该命令仅支持TUI交互;请使用
gh stack modify后重新unstack来重构堆叠。init - PR标题和描述是自动生成的。如需修改,请执行 。
gh pr edit - 仅针对本地堆叠解析。如需从GitHub拉取堆叠,请使用堆叠编号或PR编号。
checkout <branch-name>
More detail
更多细节
gh stack <command> --helpgh stack help <command>- : before creating a stack, when deciding how many layers to use and what belongs in each.
references/stack-design.md - : when a command fails unexpectedly or you need its preconditions, side effects, atomicity, or ordering guarantees.
references/commands.md - : on a rebase conflict, after a squash-merge, on local and remote divergence, or when restructuring a stack.
references/troubleshooting.md
gh stack <command> --helpgh stack help <command>- :创建堆叠前,用于决定层级数量及每个层级的内容。
references/stack-design.md - :当命令执行失败或你需要了解命令的前置条件、副作用、原子性或顺序保证时阅读。
references/commands.md - :遇到rebase冲突、合并后、本地与远程分歧或重构堆叠时阅读。
references/troubleshooting.md