Skip to content

Commit b685096

Browse files
committed
docs for modify cmd
1 parent be0aa25 commit b685096

6 files changed

Lines changed: 264 additions & 13 deletions

File tree

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,68 @@ gh stack rebase --continue
232232
gh stack rebase --abort
233233
```
234234

235+
### `gh stack modify`
236+
237+
Interactively restructure the current stack.
238+
239+
```
240+
gh stack modify [flags]
241+
```
242+
243+
Opens a terminal UI for restructuring a stack. You can rename, drop, reorder, and fold branches into adjacent ones. All the changes are staged during the preview and applied at once on save.
244+
245+
If the stack of PRs has been created on GitHub, run `gh stack submit` afterwards to push the changes and recreate the stack.
246+
247+
| Flag | Description |
248+
|------|-------------|
249+
| `--continue` | Continue after resolving conflicts |
250+
| `--abort` | Abort the modify session and restore the stack to its pre-modify state |
251+
252+
**Operations:**
253+
254+
- **Drop** (`x`): Remove a branch and its commits from the stack. Local branch and associated PR are preserved.
255+
- **Fold down** (`d`): Absorb a branch's commits into the branch below (toward trunk). Folded branch removed from stack.
256+
- **Fold up** (`u`): Absorb a branch's commits into the branch above (away from trunk). Folded branch removed from stack.
257+
- **Reorder** (`Shift+↑`/`Shift+↓`): Move a branch up (away from trunk) or down (toward trunk) in the stack.
258+
- **Rename** (`r`): Rename a branch locally and in the stack metadata.
259+
- **Undo** (`z`): Undo the last staged action.
260+
261+
**Keybindings:**
262+
263+
| Key | Action |
264+
|-----|--------|
265+
| ``/`` | Navigate branch list |
266+
| `f` | View files changed |
267+
| `c` | View commits |
268+
| `x` | Drop branch |
269+
| `r` | Rename branch |
270+
| `u/d` | Fold branch up/down |
271+
| `Shift+↑`/`Shift+↓` | Move branch up/down |
272+
| `z` | Undo last action |
273+
| `Ctrl+S` | Apply all changes |
274+
| `q`/`Esc` | Cancel and exit |
275+
| `?` | Help |
276+
277+
**Preconditions:**
278+
- Must have an active stack checked out locally
279+
- Working tree must be clean
280+
- No rebase in progress
281+
- No PR in the stack is queued for merge
282+
- Commit history must be linear
283+
284+
**Examples:**
285+
286+
```sh
287+
# Open the modify TUI
288+
gh stack modify
289+
290+
# Continue after resolving a conflict
291+
gh stack modify --continue
292+
293+
# Abort and restore to the previous state
294+
gh stack modify --abort
295+
```
296+
235297
### `gh stack sync`
236298

237299
Fetch, rebase, push, and sync PR state in a single command.

docs/astro.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default defineConfig({
6565
{ label: 'Working with Stacked PRs', slug: 'guides/stacked-prs' },
6666
{ label: 'Stacked PRs in the GitHub UI', slug: 'guides/ui' },
6767
{ label: 'Typical Workflows', slug: 'guides/workflows' },
68+
{ label: 'Restructuring Stacks', slug: 'guides/modify' },
6869
],
6970
},
7071
{
625 KB
Loading
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Restructuring Stacks
3+
description: How to use `gh stack modify` to restructure a stack.
4+
---
5+
6+
`gh stack modify` provides an interactive terminal UI for restructuring a stack locally. You can drop, fold, rename, and reorder branches and then apply all your changes at once.
7+
8+
![The modify stack terminal UI](../../../assets/screenshots/modify-stack-tui.png)
9+
10+
## When to use modify
11+
12+
Use `modify` when you need to:
13+
- **Remove** a branch from the stack
14+
- **Combine** two branches into one
15+
- **Rename** a branch
16+
- **Reorder** branches
17+
18+
## Prerequisites
19+
20+
Before running `modify`, ensure:
21+
- You have an active stack checked out locally
22+
- Your working tree is clean (no uncommitted changes)
23+
- No rebase is in progress
24+
- No PR in the stack is queued for merge
25+
- Commit history is linear (run `gh stack rebase` first if needed)
26+
27+
## Opening the TUI
28+
29+
```sh
30+
gh stack modify
31+
```
32+
33+
The TUI shows your stack as a vertical list of branches with PR information, commits, and files changed. Merged branches appear as locked rows that cannot be modified. Press `?` for a help overlay describing all operations.
34+
35+
## Operations
36+
37+
### Drop (`x`)
38+
39+
Removes a branch and its commits from the stack. The local branch and any associated PR are preserved. Upstream branches are rebased to exclude the dropped branch's unique commits.
40+
41+
### Fold down (`d`)
42+
43+
Absorbs the selected branch's commits into the branch below it (toward trunk) via cherry-pick. The folded branch is removed from the stack.
44+
45+
### Fold up (`u`)
46+
47+
Absorbs the selected branch's commits into the branch above it (away from trunk). Since the branch above already contains the folded branch's commits in its history, this is handled by adjusting what is considered the first unique commit for the branch. The folded branch is removed from the stack.
48+
49+
### Rename (`r`)
50+
51+
Opens an inline prompt to enter a new name for the branch. The branch is renamed locally and in the stack metadata. On the next `submit`, the new branch name is pushed to GitHub.
52+
53+
### Reorder (`Shift+↑`/`Shift+↓`)
54+
55+
Moves the selected branch up (away from trunk) or down (toward trunk) in the stack. A cascading rebase adjusts all affected branches. Note: reordering and structural changes (drop/fold/rename) cannot be mixed in the same session.
56+
57+
### Undo (`z`)
58+
59+
Reverses the most recent staged action. You can undo multiple times to step back through your changes.
60+
61+
## Applying changes
62+
63+
Press `Ctrl+S` to apply all staged changes. Nothing is modified until you save. The apply phase renames branches, folds/drops branches, and runs a cascading rebase to create a linear commit history with the desired stack state.
64+
65+
### Handling conflicts
66+
67+
If a rebase conflict occurs during the apply phase, you have two options:
68+
69+
1. **Resolve and continue**: Fix the conflicts in your editor, stage with `git add`, then run `gh stack modify --continue` (you may need to do this multiple times)
70+
2. **Abort**: Run `gh stack modify --abort` to abort the operation and restore the stack to the pre-modify state
71+
72+
If a second conflict occurs after continuing, the same options are available.
73+
74+
## After modifying
75+
76+
If a stack of PRs has been created on GitHub, run:
77+
78+
```sh
79+
gh stack submit
80+
```
81+
82+
This pushes the updated branches and recreates the stack. The old stack is automatically replaced.
83+
84+
## Aborting
85+
86+
If you want to discard all changes and restore the stack to its pre-modify state, run:
87+
88+
```sh
89+
gh stack modify --abort
90+
```
91+
92+
This also works if `modify` was interrupted (e.g., terminal crash). A pre-modify snapshot is cached locally for state recovery.
93+
94+
## Limitations
95+
96+
- Cannot modify merged branches (they are locked)
97+
- Cannot add new branches (use `gh stack add` instead)
98+
- Cannot split a branch into multiple branches
99+
- Cannot move branches between different stacks
100+
- Requires an interactive terminal
101+
- Reordering and structural changes (drop/fold/rename) cannot be mixed in the same session

docs/src/content/docs/guides/workflows.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,49 @@ All branches in a stack should be part of the same feature or project. If you ne
168168

169169
## Restructuring a Stack
170170

171-
When you need to remove a branch, reorder branches, or rename branches, tear down the stack and rebuild it:
171+
When you need to change the composition of a stack — remove a branch, combine branches, change the order, or rename a branch — use `gh stack modify`:
172172

173173
```sh
174-
# 1. Remove the stack on GitHub and locally
175-
gh stack unstack
176-
177-
# 2. Make structural changes
178-
git branch -m old-branch-1 new-branch-1 # rename a branch
179-
git branch -D branch-3 # delete a branch
180-
181-
# 3. Re-create the stack with the new order
182-
gh stack init --adopt new-branch-1 branch-2 branch-4
183-
184-
# 4. Push and sync the new stack
174+
# Open the modify TUI
175+
gh stack modify
176+
177+
# In the TUI:
178+
# x → drop a branch
179+
# d → fold down (into branch below)
180+
# u → fold up (into branch above)
181+
# Shift+↑/↓ → reorder
182+
# r → rename
183+
# z → undo
184+
# Ctrl+S → apply changes
185+
# q → cancel
186+
187+
# After modifying, push changes to remote and recreate the stack on GitHub
185188
gh stack submit
186189
```
187190

188-
The `unstack` command deletes the stack on GitHub first, then removes local tracking. Your branches and PRs are not affected — only the stack relationship is removed. After `init --adopt`, any existing open PRs are automatically re-associated with the new stack.
191+
### Common restructuring scenarios
192+
193+
**Remove a branch and its unique commits from the stack:**
194+
1. `gh stack modify`
195+
2. Navigate to the branch, press `x` to mark it for drop
196+
3. Press `Ctrl+S` to apply
197+
4. `gh stack submit`
198+
199+
**Combine two branches into one:**
200+
1. `gh stack modify`
201+
2. Navigate to the branch you want to fold
202+
3. Press `d` to fold its commits into the branch below, or `u` to fold into the branch above
203+
4. Press `Ctrl+S` to apply
204+
5. `gh stack submit`
205+
206+
**Reorder branches:**
207+
1. `gh stack modify`
208+
2. Navigate to the branch to move
209+
3. Press `Shift+↑` to move up or `Shift+↓` to move down
210+
4. Press `Ctrl+S` to apply
211+
5. `gh stack submit`
212+
213+
For a comprehensive guide on all modify operations, see the [Restructuring Stacks](/gh-stack/guides/modify/) guide.
189214

190215
## Using AI Agents with Stacks
191216

docs/src/content/docs/reference/cli.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,68 @@ gh stack checkout feature-auth
157157
gh stack checkout
158158
```
159159

160+
### `gh stack modify`
161+
162+
Interactively restructure the current stack.
163+
164+
```sh
165+
gh stack modify [flags]
166+
```
167+
168+
Opens an interactive terminal UI for restructuring a stack. All changes are staged in the TUI and applied together when you press `Ctrl+S`. Branches from merged PRs cannot be modified.
169+
170+
| Flag | Description |
171+
|------|-------------|
172+
| `--continue` | Continue after resolving conflicts |
173+
| `--abort` | Abort the modify session and restore the stack to its pre-modify state |
174+
175+
**Preconditions:**
176+
177+
The command checks these conditions before opening the TUI:
178+
179+
1. Must have an active stack checked out locally
180+
2. Working tree must be clean (no uncommitted changes)
181+
3. No rebase in progress
182+
4. No PR in the stack is queued for merge
183+
5. Commit history must be linear (no merge commits, no diverged branches)
184+
185+
**Operations:**
186+
187+
| Operation | Key | Effect |
188+
|-----------|-----|--------|
189+
| Drop | `x` | Remove branch and its commits from stack. Local branch and associated PR are preserved. |
190+
| Fold down | `d` | Absorb commits into branch below (toward trunk). Folded branch removed from stack. |
191+
| Fold up | `u` | Absorb commits into branch above (away from trunk). Folded branch removed from stack. |
192+
| Move down | `Shift+↓` | Reorder branch down (toward trunk) in the stack |
193+
| Move up | `Shift+↑` | Reorder branch up (away from trunk) in the stack |
194+
| Rename | `r` | Rename the branch (opens inline prompt) |
195+
| Undo | `z` | Undo the last staged action |
196+
197+
**Apply phase:**
198+
199+
When you press `Ctrl+S`, the staged changes are applied by renaming branches, folding/dropping branches, and running a cascading rebase to create a linear commit history with the desired stack state.
200+
201+
If a rebase conflict occurs, you can:
202+
- Resolve conflicts, stage files, and run `gh stack modify --continue`
203+
- Or run `gh stack modify --abort` to abort the operation and restore the stack to the pre-modify state
204+
205+
**After modifying:**
206+
207+
If a stack of PRs has been created on GitHub, run `gh stack submit` to push the updated branches and recreate the stack. The old stack is automatically replaced.
208+
209+
**Examples:**
210+
211+
```sh
212+
# Open the interactive modify TUI
213+
gh stack modify
214+
215+
# Continue after resolving a conflict
216+
gh stack modify --continue
217+
218+
# Abort and restore to the previous state
219+
gh stack modify --abort
220+
```
221+
160222
---
161223

162224
## Remote Operations

0 commit comments

Comments
 (0)