`gh stack sync` always rebases, which breaks PR review history
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.
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.
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.
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
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.
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.
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.
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:
- there is an open PR 1 from
pr_1intomain - there is an open PR 2 from
pr_2intopr_1 - PR 1 merges into
main - now PR 2 is based off
mainand 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?