Stacking and squashing PRs
24 April 2025
Delicious squash millefeuille
Stacking and squashing PRs
24 April 2025
Delicious squash millefeuille
Hello everyone. Two weeks ago there were some questions about how to deal with stacking PRs and squash merges so I wanted to come back to that and give you a couple of tips for how to deal with this.
For those who don't know, squashes are also a type of vegetable that include pumpkins and courgettes. So there will be some pictures of squashes sprinkled throughout the slides.
Pros and cons of stacking PRs
Deze slide heeft geen instructies
Agenda
What are stacked PRs?
Recommended flows (2)
Flows to avoid (2)
Weird diffs in GitHub
First we'll briefly talk about what what it actually means to stack PRs.
Then I would like to show you a couple of flows that I recommend, and a couple of flows that I don't recommend.
Finally, we'll look at weird diffs that GitHub sometimes shows.
What are stacked PRs?
1
2
To stack PRs, you need two things:
1. A branch branching off of another branch that is not develop. In the rest of the slides I will call these branches A and B.
2. A PR for that branch, where you tell GitHub that it should use A as base instead of develop.
This has two effects:
a) GitHub shows you the diff against A instead of develop in the Files Changed tab.
b) If you were to merge B first, GitHub would merge B into A instead of develop.
What we typically do instead is merge A into develop first. GitHub then automatically resets B's base to develop.
Avoid stacking PRs
When branching B off of A, ask yourself:
Is branch A my own?
Can I branch off of develop instead?
Can I wait until after A is merged?
Before we get into some tips for how to deal with stacked PRs, I first want to recommend you not stack PRs in the first place.
Stacking PRs creates extra complexity, so before you do it, ask yourself if it's really necessary.
(list items)
Let's talk about what to keep in mind if you still want to go ahead.
An unfortunate interaction
Stacked squashes
Unfortunately, stacking PRs and squashing PRs can result in new merge conflicts.
Two weeks ago, we talked about merge conflicts that arise naturally when you work together on the same codebase. We can't avoid these conflicts. Instead, we should take the time to understand them and resolve them.
But the conflicts you get from stacking and squashing are not examples of naturally arising merge conflicts. They don't represent your work diverging from someone else's work. Instead, they are a conflict between your own work and itself.
Fortunately, it's really easy to avoid these unnecessary conflicts. On the next slides I will show you how.
Updating A and B
✅ Merge develop into A
✅ Merge A into B
The first tip is to keep A and B up to date with develop.
You can merge develop into A as often as you like. Merging incrementally like this is a good idea, because it lets you solve potential merge conflicts in smaller chunks.
Similarly, you can merge A into B as often as you like. This indirectly brings changes from develop into B.
These are the yellow arrows in the diagram.
Skipping levels
❌ Avoid merging develop into B while A is alive
What I recommend you don't do is to merge develop straight into B while A is still alive.
We have asked GitHub to show B's diff relative to A. By merging develop into B and not into A, the diff becomes bigger than expected because B contains changes from develop that A doesn't have.
This can be confusing and I don't recommend it.
The death of A
Now let's say that you are done with A. The PR has been approved and we have told GitHub to merge the PR.
In this diagram we can see that GitHub has squashed A's contributions and created a new commit on develop, depicted here by the blue circle. It has also deleted its version of branch A and automatically changed B's base to be develop.
Now we might think that A's role is done, and that we can safely delete A. However, if we now merge develop straight into B, we might run into these new merge conflicts.
The death of A (premature)
❌ Don't merge squashed A straight into B
This is because Git is unaware that A and the squashed version of A represent the same work.
Merging like this might result in a conflict that could have been avoided.
For this reason, I don't recommend that you merge develop — which contains squashed A — straight into B like this.
So what should you do instead?
Update A and B one more time
✅ Update A and B one more time after squash-merging A
The solution is really easy. Just go through the updating motion one more time.
GitHub may have deleted its version of A, but you still have your local branch A.
Merge develop into A one more time, and merge A into B one more time.
Now, you can safely delete A and merge develop straight into B.
In a way, this commit teaches Git that A and squashed A represent the same work.
GitHub: avoiding weird diffs
Your diff on GitHub might look strange if:
B contains changes from develop that are not in A
GitHub's B is more up to date than A
GitHub's B is from before A was merged
Solution:
if A still exists, update A and push
update B and push
Finally, let's talk about when GitHub shows weird of bigger diffs than you're expecting.
Conclusion
Avoid stacking PRs if you can
After squash-merging, update A and B one more time to avoid unnecessary conflicts
That's it.
In conclusion: