Skills101

AdvancedGit & GitHub · Lesson 8 of 9 · Updated ·

Rebase vs Merge and Clean Git History — Squash, Fast-Forward, Cherry-Pick

In an earlier lesson you saw that merge keeps two lines of history and rebase makes a straight line. This lesson goes deeper: what rebase actually does to your commits, the different ways a pull request can be merged, and how professionals keep main readable. All outputs below come from running the commands for real; your commit ids will be different.

1. Why history quality matters

git log is the project's diary. Six months later, someone asks "why did the cart total change?" A history like this helps:

Add coupon codes at checkout (#42)
Fix GST rounding on invoices (#41)
Add cart page (#40)

A history like this does not:

wip
fix
fix again
asdf
final final

Clean history also makes git bisect (finding the commit that introduced a bug), git revert and code review much easier.

2. Our starting point

main has two commits. You branch feature/cart and add two commits. Meanwhile a teammate adds "Fix typo in footer" to main.

git log --oneline --graph --all
* f4f9a02 Fix typo in footer
| * 92adf28 Add cart page
| * 5686c50 Add cart model
|/  
* f59f455 Add footer
* 4eb3f13 Add home page

You now have two choices for bringing your work up to date.

3. Option A: merge main into your branch

git switch feature/cart
git merge main

This creates a merge commit with two parents. Nothing existing is changed. It is always safe, even on a branch other people use, but if you do it often your branch fills with "Merge branch 'main' into feature/cart" commits.

4. Option B: rebase your branch onto main

git switch feature/cart
git rebase main
git log --oneline --graph --all
Successfully rebased and updated refs/heads/feature/cart.
* b1e9528 Add cart page
* 2d43900 Add cart model
* f4f9a02 Fix typo in footer
* f59f455 Add footer
* 4eb3f13 Add home page

Before the rebase, feature/cart commits 5686c50 and 92adf28 branch off f59f455 while main moved to f4f9a02. After the rebase, the same two changes sit on top of f4f9a02 as new commits 2d43900 and b1e9528, in a straight line

Look closely at the ids. "Add cart model" was 5686c50; now it is 2d43900. Rebase does not move commits — it creates new ones:

  1. Find the commits on your branch that are not on main (5686c50, 92adf28).
  2. Temporarily set them aside.
  3. Move your branch to the tip of main (f4f9a02).
  4. Re-apply each change, one at a time, as a new commit with a new parent — and therefore a new id.

A commit's id is calculated from its content and its parent. A new parent means a new id, even if the code change is identical.

The golden rule

Never rebase commits that other people already have.

If a teammate built on top of 5686c50 and you replace it with 2d43900, their history and yours now disagree, and they get confusing duplicate commits. Rebase your own local or personal branch; merge shared branches.

5. Fast-forward merges

After the rebase, main is a direct ancestor of feature/cart. Merging needs no merge commit — Git just slides the main label forward:

git switch main
git merge --ff-only feature/cart
Updating f4f9a02..b1e9528
Fast-forward
 cart.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 cart.txt

--ff-only means "merge only if it can be a fast-forward; otherwise refuse". Teams that want a perfectly linear main use it as a safety check.

6. Squash merges

Your branch often contains commits like "WIP", "fix typo" and "address review". Nobody needs those on main. A squash merge combines all the branch's changes into one new commit:

git switch main
git merge --squash fix/coupon
git commit -m "Add coupon codes at checkout (#42)"
git log --oneline -3
cb59cb8 Add coupon codes at checkout (#42)
b1e9528 Add cart page
2d43900 Add cart model

Three branch commits named WIP coupon, fix typo and address review become a single commit on main called Add coupon codes at checkout (#42)

git merge --squash stages the combined changes but does not commit — you write one clear message yourself.

7. The three GitHub merge buttons

On a pull request, GitHub offers three ways to merge:

ButtonWhat lands on mainHistory
Create a merge commitall branch commits + a merge commitfull detail, not linear
Squash and mergeone new commit with all changeslinear, one commit per PR
Rebase and mergeeach branch commit re-created on top of mainlinear, every commit kept

Many teams choose squash and merge: each pull request becomes one tidy, revertable commit. Others prefer merge commits so the full story is kept. Ask what your team uses — both are reasonable.

8. Tidying your own commits before a pull request

Before you open a PR, you may want to clean your own, not-yet-shared commits:

  • Fix the last commit (message or a forgotten file): git commit --amend.
  • Fold a small fix into an earlier commit: git commit --fixup <commit>, then later git rebase -i --autosquash main combines it automatically.
  • Reorder, combine or reword several commits: git rebase -i main opens a list of your commits where you change pick to squash, reword or drop. (This interactive mode opens an editor; it is worth practising on a test repository first.)

If you already pushed the branch, rewriting it means you must force-push. Use the safer form:

git push --force-with-lease

Plain --force overwrites whatever is on the remote. --force-with-lease refuses if someone else pushed to the branch since you last fetched, so you cannot silently delete their work.

9. cherry-pick: copy one commit

Sometimes you need one commit from another branch — for example, a bug fix from develop that must go to main today:

git switch main
git cherry-pick 6a74bae

Git re-applies that commit's change on your branch as a new commit (new id, same change). Use it for occasional fixes, not as a replacement for merging whole branches.

10. Merge or rebase? A quick guide

SituationUse
Update your own feature branch with the latest mainrebase (or merge, if you prefer)
A branch that others also push tomerge
Bring a finished feature into mainthe team's PR setting (often squash)
Need just one fix from another branchcherry-pick
Already pushed and someone may have pulled itnever rebase; use merge or revert

Common mistakes

  • Rebasing a shared branch, then force-pushing over teammates' commits.
  • Using git push --force when --force-with-lease would protect others' work.
  • Thinking rebase "moves" commits — it creates new ones, so the old ids disappear from the branch.
  • Squashing a huge branch that mixed unrelated changes; split it into separate PRs instead.

Quick recap

  • Merge keeps history and adds a merge commit; always safe.
  • Rebase re-creates your commits on a new base: new ids, straight line. Never rebase commits others have.
  • --ff-only merges only when no merge commit is needed.
  • Squash merge turns a whole branch into one clean commit on main.
  • Clean your own commits with amend, fixup and interactive rebase; push with --force-with-lease.
  • cherry-pick copies a single commit.

Practice what you learned

📝 Quiz: Rebase vs Merge and Clean History5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →