Stacked Diffs with git rebase —onto

https://news.ycombinator.com/rss Hits: 5
Summary

tldr; Use git rebase --onto to cleanly rebase a dependent branch without dragging along commits that don’t belong to it. 1git rebase --onto <new-base> <old-base> <branch> If you’ve ever worked on a larger feature and split your work into multiple PRs that depend on each other, you’ve probably experienced the pain of keeping them in sync. This workflow is called stacked diffs (or stacked PRs), and it’s incredibly powerful. But it comes with a learning curve. The secret weapon? git rebase --onto. Here’s what we’ll cover: Why stacked diffs are worth the effort The difference between a regular git rebase and git rebase --onto Step-by-step: first sync, ongoing syncs, and post-merge cleanup # Why Stacked Diffs? Let’s say you’re building a large feature. You could dump everything into one massive PR, but reviewers hate that. Large PRs get superficial reviews (or no reviews at all), and you end up waiting forever for approvals. Stacked diffs solve this by breaking your work into smaller, dependent PRs: main └── feature-1 (auth layer) └── feature-2 (user profile) └── feature-3 (profile settings) Each PR is small, focused, and easy to review. The catch? When main updates or when feature-1 gets rebased, you need to sync all the downstream branches. That’s where most people get stuck. # Regular rebase vs rebase –onto # Regular rebase A regular git rebase main replays your commits on top of the target branch: Before: main: A---B---C \ feature: D---E After git rebase main: main: A---B---C \ feature: D'---E' Simple enough. But what happens with stacked branches? # The Problem with Stacked Branches Here’s a typical stacked setup: main: A---B---C \ feature-1: D---E \ feature-2: F---G Now main gets updated with new commits: main: A---B---C---H---I \ feature-1: D---E \ feature-2: F---G You rebase feature-1 onto main: main: A---B---C---H---I \ \ old: D---E D'---E' ← feature-1 (new hashes!) \ feature-2: F---G ← Still based on old D---E! See the problem? feature-2 is still based on the o...

First seen: 2025-12-05 11:16

Last seen: 2025-12-05 15:16