Algocraft / Git Commands / git rebase

🪄

git rebase

Replay your branch's commits onto a new base, one by one — every replayed commit gets a brand-new hash

Linear History History Rewrite New Hashes
Commit Graph Step 0 / 0
Working Directory → Staging → Repository
Terminal Transcript

Key Concepts

🎯 When to Use
  • Cleaning up a feature branch before opening a PR
  • Keeping a linear, easy-to-read history on solo/local branches
  • Pulling in the latest main without a noisy merge commit
⚖️ Rebase vs Merge
  • Merge preserves exact history + a two-parent commit
  • Rebase rewrites commits onto a new base — linear, but new hashes
  • The "golden rule": never rebase a branch others have pulled from
🎓 Pro Tips
  • Interactive rebase (-i) lets you reorder, squash, or edit commits as they replay
  • After rebasing a pushed branch, you'll need push --force-with-lease
  • If it gets messy, git rebase --abort puts you right back where you started