[Bug]: Sync/rebase fail when a submodule change is added to the trunk
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
- Update a submodule in the trunk branch (i.e. merge a PR with a submodule update).
- Run
gh stack rebase - 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 stackand reproduced this issue on the latest stable release.
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 rebaserebases the first branch, then fails with "uncommitted changes in worktree" becausegit rebaseupdates the gitlink in the index but not the submodule checkout.gh stack rebase --aborthits the same check, so recovery needsgit submodule updatebefore each abort attempt.Plain
git rebasetolerates this state, and the stack can be finished by rebasing each branch withgit rebase --ontoand runninggit submodule updatebefore each one.A warning for anyone trying to work around it by setting
submodule.<name>.ignore=all(e.g. throughGIT_CONFIG_*env vars): it getsgh stack rebasethrough, 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 updatefor 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.
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
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".