Stop 03 · Git & GitHubTopics · 6/11

Rebase

Part of the Backend Developer Roadmap

In short

Rebase gives you a clean, straight history, as if you started your work on today's main. Powerful, but it rewrites commits, so learn the one golden rule.

`git rebase` replays your branch's commits on top of another branch's latest history, producing a cleaner, linear history than a merge would.

The mascot lifts a small stack of cards onto a taller, newer pile.

Think of it like this

Restacking your cards

You made a small pile of cards on top of an old deck. Meanwhile, the deck got new cards. Rebase lifts your pile off and places it on top of the new deck, one card at a time. Same cards, new base.

Watch it happen

Replaying your commits on new main

push(M1 (main))
M1 (main)

Top of the stack is coloured

1/5main is at M1. You branch off here.

The big ideas

What you need to know

Linear history

No merge commits, just one straight line. git log reads like a clean story.

New hashes

Replayed commits are new commits. Anyone who had the old ones now has a different history.

The golden rule

Never rebase commits others have already pulled. Rebase only your own local or private branch.

Interactive tidy-up

git rebase -i lets you squash, reword or drop your own commits before opening a pull request.

See it in code

Walk through the code

bash
1git switch feature/orders2git fetch origin3git rebase origin/main          # replay my commits on latest main4 5# If a conflict stops it:6git status                      # fix the files, then7git add src/orders.js8git rebase --continue           # or: git rebase --abort9 10git push --force-with-lease     # update MY branch on GitHub

1/3Get the latest main from GitHub and replay your feature commits on top of it.

--force-with-lease refuses to overwrite the remote branch if someone else pushed to it. Plain --force doesn't check.

Save yourself the pain

Common mistakes

Oops

Rebasing main or a shared branch and force-pushing it.

Do this instead

Rebase only branches that only you work on. For shared branches, merge instead.

Oops

Using git push --force after a rebase.

Do this instead

Use --force-with-lease. It stops you from wiping a teammate's push you didn't know about.

Your mission

Rebase versus merge

~25 min

Quick check

Why is it risky to rebase a branch your teammates have already pulled?

Go deeper

  1. 1Rebase a feature branch onto an updated `main` and compare the log to a merge.
  2. 2Learn the golden rule: never rebase commits that have already been pushed and shared.
  3. 3Practice an interactive rebase (`git rebase -i`) to squash a few commits together.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge