Git for EngineersRebasing Deep Dive

Rebasing Deep Dive

Rewrite history cleanly with git rebase.

~20 min read

Rebasing Deep Dive

Rebase replays your commits on top of another branch, creating a linear history.

git rebase main

Interactive rebase

git rebase -i HEAD~3 opens an editor where you can:

  • pick — keep the commit as-is
  • squash/fixup — combine with previous commit
  • reword — edit the commit message
  • drop — delete the commit entirely

When to use rebase vs merge

| Scenario | Use | |---|---| | Integrating upstream changes locally | rebase | | Merging a feature into main | merge (or squash merge) | | Shared branch others are working on | Never rebase |

Golden rule

Never rebase commits that have been pushed to a shared branch. It rewrites history and forces others to reconcile diverged histories.