Skip to content

[Bug]: rebase --no-trunk rebases the lowest open branch onto trunk when the branch below it is merged #642

Backlog

Description

@nmussy

Describe the bug

gh stack rebase --no-trunk is documented as "Skip trunk — only rebase stack branches onto each other". When the branch below the stack's lowest open branch is merged, it still rebases that open branch onto the local trunk, then reports rebased locally (without trunk). The local trunk is often stale when it's checked out in another worktree, so the next gh stack push force-pushes an untouched bottom PR onto an outdated trunk.

Likely cause: in runRebase (cmd/rebase.go), --no-trunk only skips index 0 (if opts.noTrunk && startIdx < 1 { startIdx = 1 }), but s.Branches still holds merged branches. The merged predecessor sets needsOnto, and cascadeRebase falls back to trunkRef because no non-merged ancestor exists. Starting the range after s.FirstActiveBranchIndex(), with syncStackPRs called before the range is computed, fixes the repro below. I haven't run the test suite against that change.

Related: #491 (also a stale local trunk, on sync).

Affected command

gh stack rebase --no-trunk

gh-stack version

gh stack version 0.3.0

GitHub CLI version

gh version 2.103.3 (2026-10-26)

Operating system

macOS 26.1

Shell

zsh

Steps to reproduce

  1. Have a stack whose bottom branch's PR is merged, and whose trunk has moved on since.
  2. Commit on a middle branch.
  3. Run gh stack rebase --no-trunk.

The script below does this offline in a temp directory (needs git, jq and gh-stack). Its jq line stands in for the GitHub API by recording b0's PR as merged.

Repro script
#!/usr/bin/env bash
set -euo pipefail
cd "$(mktemp -d)"
c() { echo "$1" > "$1.txt"; git add "$1.txt"; git commit -qm "$1"; }

git init -q --bare -b main origin.git
git clone -q origin.git primary 2>/dev/null && cd primary
git config user.email repro@example.com && git config user.name repro
c M1 && git push -q origin main

# Stack main <- b0 <- b1 <- b2 <- b3, in a linked worktree
git worktree add -q -b b0 ../stack main && cd ../stack
c B0; git checkout -qb b1; c B1; git checkout -qb b2; c B2; git checkout -qb b3; c B3
gh stack init b0 b1 b2 b3 >/dev/null

# b0's PR merges; trunk moves on to M2
cd ../primary
git merge -q --no-ff -m "Merge b0" b0 && c M2 && git push -q origin main
cd ../stack

# Stand-in for the GitHub API: record b0's PR as merged
F="$(git rev-parse --git-common-dir)/gh-stack"
jq --arg h "$(git rev-parse b0)" \
  '.stacks[0].branches[0].pullRequest = {number: 1, url: "https://example.invalid/1", merged: true, mergedHead: $h}' \
  "$F" > "$F.new" && mv "$F.new" "$F"

# Add a commit to b2 and cascade it upward without touching trunk
git checkout -q b2 && c FIX
echo "b1 sits on: $(git log -1 --format=%s b1~1)"
gh stack rebase --no-trunk
echo "b1 sits on: $(git log -1 --format=%s b1~1)   (expected: B0)"

Expected behavior

b1 stays on B0, and only b2 and b3 are rebased.

Actual behavior

b1 is rebased onto the local main (M2), and the command still reports it rebased without trunk.

Terminal output

b1 sits on: B0
Stack detected: (main) <- b0 <- b1 <- b2 <- b3
Rebasing branches in order, starting from b1 to b3
✓ Rebased b1 onto main (adjusted for merged PR)
✓ Rebased b2 onto b1 (adjusted for merged PR)
✓ Rebased b3 onto b2 (adjusted for merged PR)
Skipped 1 merged branch: b0
All branches in stack rebased locally (without trunk)
To push up your changes, run `gh stack push`
b1 sits on: M2   (expected: B0)

Preflight checks

  • I searched the existing issues for duplicates.
  • I ran gh extension upgrade stack and reproduced this issue on the latest stable release.

🤖 Authored by Claude Code

Activity

  1. nmussy commented on Nov 23, 2026

    @nmussy
    Author

    Here is the patch mentioned above, against v0.3.0. With it applied, the repro leaves b1 on B0 and rebases only b2 and b3. I haven't run the test suite against it.

    diff --git a/cmd/rebase.go b/cmd/rebase.go
    index 2d954ce..cd459e5 100644
    --- a/cmd/rebase.go
    +++ b/cmd/rebase.go
    @@ -177,15 +177,17 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
     	if opts.upstack {
     		startIdx = currentIdx
     	}
    -	if opts.noTrunk && startIdx < 1 {
    -		startIdx = 1
    +	_ = syncStackPRs(cfg, s)
    +	if opts.noTrunk {
    +		if i := s.FirstActiveBranchIndex(); i >= 0 && startIdx <= i {
    +			startIdx = i + 1
    +		}
     	}
    -	branchesToRebase := s.Branches[startIdx:endIdx]
    -	if len(branchesToRebase) == 0 {
    +	if startIdx >= endIdx {
     		cfg.Printf("No branches to rebase")
     		return nil
     	}
    -	_ = syncStackPRs(cfg, s)
    +	branchesToRebase := s.Branches[startIdx:endIdx]
     	ctx, err := worktree.New()
     	if err != nil {
     		cfg.Errorf("%s", err)

    syncStackPRs moves up so the merged flags are current when the range is chosen. It now also runs before the "No branches to rebase" early return.

    🤖 Authored by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions