|
| 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 | + |
| 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 |
0 commit comments