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

`gh stack sync` always rebases, which breaks PR review history

Open dbushong opened this issue 4 months ago • 7 comments

If a reviewer has already viewed/reviewed a PR, and the submitter performs a gh stack sync, it will then force-push, which erases useful information about "changes since last view" in the PR interface.

It should be an option (the default?) to perform a merge from upstream rather than a rebase to avoid messing up the review history.

dbushong avatar Jul 29 '26 17:07 dbushong

EDIT: Okay, there's a way to make git merge work :) https://github.com/github/gh-stack/issues/354#issuecomment-5338629751

It would be even better to make GitHub able to show review history after a force-push. This has always been the obstacle to stacking (which you can otherwise do in pure git with one-commit-per-PR and git rebase --update-refs, without even needing rerere because of git rebase's cherrypick logic).

Merging by default is probably not a good idea because you'll hit merge conflicts when combining merging, stacking and squashing. Try this:

# Create a fresh git repo in a new directory.
git init
git commit --allow-empty -m "initial commit"


# Make a stack: main -> parent -> child
git switch -c parent
echo "version 1" > file.txt
git add file.txt && git commit -m "file version 1"

git switch -c child
echo "version 2" > file.txt
git add file.txt && git commit -m "file version 2"


# "Merge" parent as if using the Squash & Rebase strategy:
git switch main
git merge --squash parent
git commit -m "squash parent onto main"


# Now try merging main into child.
# EDIT: See my other comment below for the right thing to do instead!
git switch child
git merge main

You'll hit a conflict on git merge main:

++<<<<<<< HEAD
 +version 2
++||||||| 0c635e0
++=======
+ version 1
++>>>>>>> main

This will happen even with rerere enabled.

timoffex avatar Jul 29 '26 17:07 timoffex

I think with stacking, that is a side effect you have to live with.

You can't even merge multiple branches of a stack at one time if you have "require approval of the most recent reviewable push" with "squash and merge" anyway. Stacked PRs are constantly rebasing if you modify any layers of the stack. That's just how it works.

MelDommer avatar Jul 31 '26 15:07 MelDommer

If I manually:

$ git checkout main
$ git pull
$ git checkout my-current-pr-branch
$ git merge main
$ git push

Then I have successfully avoided annoying all of the reviewers of my PR. I don't see why this is not a valid thing to automate.

(Side note: that's currently git push -u origin my-current-pr-branch the first time because of #331 )

I don't disagree with:

It would be even better to make GitHub able to show review history after a force-push

but it's been years and I assume that's a bigger ask

dbushong avatar Jul 31 '26 15:07 dbushong

Yeah without relying on merge commits, I think the only viable solution is for Github to snapshot the branch on push, so that the context for comments and CI is never lost even if you force-push.

*Unclear to me if there's a way to do this with just its own special branches, or if Github would need to implement a separate snapshotting system orthogonal to git.

kelu-wandb avatar Jul 31 '26 21:07 kelu-wandb

I had hoped gh stack would either:

  • Come with UI improvements that made force-push to PRs workable, or
  • Use more branches to allow merge-only stacks

That it does neither seems like a huge oversight, and basically makes it useless. The issue with stacking manually or with the many existing tools is that it breaks just about every feature designed to make reviewing a PR tenable.

Yeah without relying on merge commits, I think the only viable solution is for Github to snapshot the branch on push, so that the context for comments and CI is never lost even if you force-push.

This would be a step in the right direction, but even if you emulate this today by e.g. pushing refs to save all previous versions of the PR branch from garbage collection, there is no way to see the comment in the original context outside of the small snippet displayed inline. The link to view the "Reviewed Changes" or whatever just goes to the latest version, and even then only seems to work in some narrow cases.

scott-linder avatar Aug 08 '26 22:08 scott-linder

Ah, I'd like to take back what I wrote earlier about git merge causing conflicts in stacks. The trick to having a conflict-free git merge workflow is to merge main back into the local PR branch after squash-merging the PR, and then merge that into its descendants.

So in my example, after parent is merged you run:

git switch parent
git merge main
git switch child
git merge parent

instead of trying to merge main directly into child. This avoids all stack-related conflicts! After that, you can delete the local parent branch.

If gh-stack were to default to merging instead of rebasing, it should do something like this automatically.

timoffex avatar Aug 19 '26 06:08 timoffex

in the meanwhile, i implemented gh stack pull in an own fork. it mirrors the options of gh stack rebase, but uses merge instead (in exactly the way described above). you can try it with gh extension install misha-antonenko/gh-stack. i plan to support this for a while

note that this still doesn't solve the following problem on GH side in case your repo uses squash-and-merge:

  1. there is an open PR 1 from pr_1 into main
  2. there is an open PR 2 from pr_2 into pr_1
  3. PR 1 merges into main
  4. now PR 2 is based off main and has all the same conflicts and automatically requested reviewers as PR 1 🥴

the only solution that i see here is make sure that PR 2 is a draft when PR 1 is merged, then call gs pull, then gs push. or does anyone know a better one?

misha-antonenko avatar Sep 05 '26 07:09 misha-antonenko