If you work in a team that stacks branches or uses Squash & Merge, you have almost certainly encountered this nightmare:
- You create branch
feature-Bofffeature-Awhile waiting for review. -
feature-Agets approved and squash-merged intomain. - You open a Pull Request for
feature-Bagainstmain. - Disaster: Your PR suddenly includes all 20 commits from
feature-A, hundreds of duplicate lines of diff, and a maze of merge conflicts.
A standard git rebase main often gets confused because the commit hashes of feature-A no longer exist on main (they were replaced by a single squash commit).
This is where Git's most underrated surgical tool comes in: git rebase --onto.
The Mental Model: Git as Scissors & Glue
A standard rebase says:
"Take everything on my branch since it diverged from
main, and replay it on top ofmain."
The --onto flag gives you precision scissors:
"Take only the commits between point X and point Y, cut them out, and glue them onto point Z."
Syntax Breakdown
bash git rebase --onto <new-base> <old-base> [<branch>] 
![Why Object.keys(user) Returns string[], Not (keyof User)[]: TypeScript Means It](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2q4w48b9bzs43tynyhsf.png)