Rebase, Cherry-Pick & History Surgery
Rebase, Cherry-Pick & History Surgery
Every senior engineer eventually faces the same decision: should I merge this branch or rebase it? The choice shapes not just the graph you see in git log — it shapes how your team debugs incidents, bisects regressions, and reviews history six months from now. This lesson moves beyond the basics and teaches you to wield Git's history-manipulation tools the way staff engineers and DevOps leads do at scale.
Merge vs Rebase: The Mental Model
A merge records the fact that two lines of development converged at a specific moment. It preserves causality: you can always reconstruct exactly what was on main when your feature landed. A merge commit has two parents and is the permanent record of that join.
A rebase takes your commits and replays them on top of a different base. The commit content is identical, but the SHA changes because the parent pointer changes. The result looks as though you started your work after the latest upstream commit — the branch becomes linear.
Everyday Rebase: Keeping a Feature Branch Current
The most common use of rebase is simple: you want your feature branch to sit on top of the latest main before you open a pull request.
Interactive Rebase: The Surgeon's Scalpel
Interactive rebase (git rebase -i) lets you rewrite history before it reaches the shared remote. At big-tech companies, squashing WIP commits into atomic, reviewable commits before pushing is standard practice. The commands available per commit are:
- pick — keep the commit as-is
- reword — keep the commit but edit its message
- edit — pause after applying so you can amend content
- squash — merge into the previous commit, combining messages
- fixup — like squash but discard this commit's message
- drop — remove the commit entirely
git config --global core.editor "code --wait" (VS Code) or vim. Many teams also use git rebase -i --autosquash together with git commit --fixup=<SHA> to automatically order fixup commits during the rebase.
Cherry-Pick: Transplanting Individual Commits
Sometimes you need a specific fix from one branch on another — without merging the entire branch. git cherry-pick applies the diff of one or more commits onto your current HEAD.
Cherry-picked from main: 9d1e2f3) and track them in your release notes.
Reflog: The Emergency Undo
The reflog is Git's hidden safety net. Every time HEAD moves — commit, rebase, reset, checkout — Git records the old position in .git/logs/HEAD. This log persists for 90 days by default and is local only (not pushed). It means almost nothing is truly lost on your machine.
Other History Surgery Tools
git commit --amend rewrites the most recent commit — useful to fix a typo in a commit message or add a forgotten file. Like rebase, it changes the SHA, so only use it before pushing.
git reset moves the branch pointer backward. --soft keeps your changes staged; --mixed (default) unstages them; --hard discards them entirely. Use --hard only when you are certain you want to throw away work.
git bisect is the partner tool to clean history: a linear, atomic commit history makes binary search for regressions dramatically faster. This is why teams enforce "one logical change per commit" — not aesthetics, but incident response efficiency.