π Handbook Home Β Β·Β β¬ οΈ Prev: Undoing Changes Β Β·Β Next: Advanced Git β‘οΈ
Sometimes you want to reshape history rather than just undo it β to clean up messy commits before sharing, or to keep history tidy and linear. That's what rebase and friends are for. These are power tools: sharp, useful, and worth respecting.
Rebasing changes commit hashes. If you rewrite commits that teammates already have, your histories diverge and you'll create a painful mess. So:
- β Rewrite freely on your own local, unpushed branch.
- β Don't rebase
mainor any shared branch others are building on.
Keep that in mind and rebasing is safe and wonderful.
A merge ties two branches together with a merge commit. A rebase instead moves your branch's commits so they start from the tip of another branch β producing a clean, linear history.
Merge: Rebase:
A β B β C βββββ M (main) A β B β C β D' β E' (linear!)
β² β± (feature replayed on top of C)
D β E (feature)
git switch feature
git rebase main # replay feature's commits on top of main's latestIf a conflict appears mid-rebase:
# fix the conflicted files, then:
git add <files>
git rebase --continue # proceed to the next commit
git rebase --skip # skip the current commit
git rebase --abort # cancel and return to where you started| Merge | Rebase | |
|---|---|---|
| History | Preserves exactly what happened (with merge commits) | Clean, linear, easy to read |
| Safety on shared branches | β Safe | |
| Traceability | Shows true branching | Hides the original branch points |
Many teams rebase local feature branches to tidy them, then merge into main via a Pull Request. Both are valid β it's a team style choice.
git rebase -i lets you edit, reorder, squash, and delete your recent commits before sharing them. This is how you turn 6 messy "wip", "fix", "oops" commits into 1β2 clean ones.
git rebase -i HEAD~4 # interactively edit the last 4 commitsGit opens an editor listing those commits:
pick a1b2c3d Add login form
pick b2c3d4e wip
pick c3d4e5f fix typo
pick d4e5f6a oops forgot file
Change the word before each commit to tell Git what to do:
| Command | Does |
|---|---|
pick |
Keep the commit as-is |
reword |
Keep the commit, but edit its message |
edit |
Pause to amend the commit's contents |
squash |
Merge into the previous commit, combining messages |
fixup |
Like squash, but discard this commit's message |
drop |
Delete the commit entirely |
| (reorder lines) | Reorder the commits |
Example β squash the three messy commits into the first:
pick a1b2c3d Add login form
fixup b2c3d4e wip
fixup c3d4e5f fix typo
fixup d4e5f6a oops forgot file
Save and close β you now have one clean commit: "Add login form." β¨
π‘ The
git commit --fixup <hash>+git rebase -i --autosquashcombo automates this: mark fixups as you go, then let Git arrange them.
Copy an individual commit from one branch onto your current branch β useful for hotfixes or pulling one change without merging a whole branch.
git cherry-pick 9c6c1f4 # apply that commit here
git cherry-pick A^..B # apply a range of commits
git cherry-pick --no-commit 9c6c1f4 # apply changes but don't commit yetmain: A β B β C
β² cherry-pick X
feature: β¦ β X β Y βββΆ main: A β B β C β X'
| Tool | What it does | History |
|---|---|---|
revert |
New commit that undoes an old one | Preserved (safe) |
reset |
Moves branch pointer back | Rewritten (local) |
rebase |
Replays commits onto a new base | Rewritten (local) |
- Golden rule: never rewrite history others have pulled.
git rebase maingives a linear history vs. a merge commit.- Interactive rebase (
-i) lets you squash/reword/reorder/drop commits β perfect for cleaning a branch before a PR. git cherry-pickcopies a single commit onto your branch.- Mid-rebase:
--continue,--skip, or--abort.
π Handbook Home Β Β·Β β¬ οΈ Prev: Undoing Changes Β Β·Β Next: 9 Β· Advanced Git β‘οΈ