gh-stack icon indicating copy to clipboard operation
gh-stack copied to clipboard

[Bug]: Sync/rebase fail when a submodule change is added to the trunk

Open 4wrxb opened this issue 1 month ago • 3 comments

Describe the bug

Stack seems to completely break when asubmodule is updated in main. A rebase can be successful, but because the submodule is not updated by rebase the worktree now has uncommitted changes that causes the next rebase to fail.

The resulting state cannot be continued (after updating the submodule) and instead must be aborted which undoes the successful rebase. The only workaround I have found is to perform a downstack rebase one PR at a time and update the submodules in between.

Affected command

gh stack rebase

gh-stack version

0.2.1

GitHub CLI version

2.94.0

Operating system

Rocky Linux 8.10

Shell

zsh and bash

Steps to reproduce

  1. Update a submodule in the trunk branch (i.e. merge a PR with a submodule update).
  2. Run gh stack rebase
  3. After the first rebase the gh stack command will fail on uncommitted changes because the submodule is out of date

Expected behavior

The stack will rebase successfully.

Actual behavior

After the first successful rebase, changes are detected in the submodules and gh stack aborts.

Terminal output

> gh stack rebase
✓ Fetched latest main from chips
Stack detected: (main) <- branch1 <- branch2 <- branch3
Rebasing branches in order, starting from branch1 to branch3
✓ Rebased branch1 onto main
✗ could not start rebase of branch2 onto branch1: preparing branch2: uncommitted changes in worktree /path/to/repo; commit or stash them before continuing
✗ Could not fully restore the stack; recovery state was retained: restoring branch1 in /path/to/repo; uncommitted changes in worktree /path/to/repo; commit or stash them before continuing
uncommitted changes in worktree /path/to/repo; commit or stash them before continuing
> git status
On branch branch1
Your branch and 'origin/branch1' have diverged,
and have 12 and 2 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   third_party/submodule (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

Preflight checks

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

4wrxb avatar Oct 12 '26 20:10 4wrxb

The horse's mouth says:

Reproduced on gh-stack 0.3.0 with a minimal setup: a repo with one submodule, a two-branch stack based on a trunk commit with the old pin, then a trunk commit that bumps the pin. gh stack rebase rebases the first branch, then fails with "uncommitted changes in worktree" because git rebase updates the gitlink in the index but not the submodule checkout. gh stack rebase --abort hits the same check, so recovery needs git submodule update before each abort attempt.

Plain git rebase tolerates this state, and the stack can be finished by rebasing each branch with git rebase --onto and running git submodule update before each one.

A warning for anyone trying to work around it by setting submodule.<name>.ignore=all (e.g. through GIT_CONFIG_* env vars): it gets gh stack rebase through, but it also makes patch-id ignore gitlinks. A commit that only changes the pin then looks identical to the trunk's pin bump and is silently skipped as an already-applied cherry-pick.

A possible fix: after rebasing each branch, run git submodule update for submodules whose checkout matched their pin before that branch was rebased. Those are only lagging behind the pin, while a submodule with real local changes would still block.

lionel- avatar Oct 28 '26 06:10 lionel-

It doesn't seem like this repo allows opening PRs, but here is a fix which I have tested locally: https://github.com/github/gh-stack/compare/main...mattklein123:gh-stack:fix-submodule

mattklein123 avatar Nov 10 '26 16:11 mattklein123

I also reproduced this on gh-stack v0.3.0 / gh 2.104.0 on Windows 11 (PowerShell 7), with a stack of four open PRs after the trunk bumped a submodule pin. Same output: the first branch rebases, the next fails on "uncommitted changes in worktree", and gh stack rebase --continue then reports "restoration is incomplete; run gh stack rebase --abort".

adaviding avatar Nov 21 '26 22:11 adaviding