Gitastic
Path › Part 2: Branching › Lesson 7 of 16
07

Rebase: replaying commits

Move your work onto a new starting point for a straight-line history.

1 Learn the idea

Merge ties two histories together with a merge commit. Rebase takes another approach: it takes your commits and replays them, one by one, on top of another branch — as if you had started your work from there.

What actually happens

  1. Git finds the commits on your branch that are not on the target (e.g. main).
  2. It moves to the tip of the target and re-applies each commit as a new commit.
  3. Finally it moves your branch pointer to the last new commit.
  4. The old commits are left behind, unreferenced (they fade out of view but can be recovered via the reflog).

2 Watch it happen

Replaying feature on top of main

Press Play or use → to step through. Watch the graph as each command runs.

b01499b Initialb01499bInitiala63097b Feature part 1a63097bFeature part 1cc6c982 Feature part 2cc6c982Feature part 2de2e298 Main moves onde2e298Main moves onBranch "feature" — a movable pointerfeatureHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git switch feature

    feature branched off before main got Main moves on. The histories have diverged.

  2. $ git log --oneline --all

    Note the hashes of the two feature commits. They are about to be replaced.

  3. $ git rebase main

    Rebase replays both feature commits on top of main. They get new hashes. The originals are faded — orphaned, no branch points to them anymore.

  4. $ git log --oneline

    A perfectly linear history: Initial → Main moves on → Feature part 1 → Feature part 2. No merge commit.

  5. $ git switch main

    Back to main. Since feature now sits directly on top of main…

  6. $ git merge feature

    …merging is a clean fast-forward. This rebase-then-fast-forward flow is how many teams keep history straight.

  7. $ git branch -d feature

    Done. History reads like the work happened in a single line.

Merge
  • Keeps history exactly as it happened
  • Adds a merge commit with two parents
  • Never rewrites existing commits — always safe
  • History can look like a subway map
Rebase
  • Rewrites your commits onto a new base
  • No merge commit — straight line
  • Creates new hashes — only for unshared work
  • Easier to read, git log tells a clean story

Rebase conflicts

Because commits are replayed one at a time, a conflict can stop the rebase mid-way. Resolve it just like a merge conflict — edit the file, git add — but then run git rebase --continue (not commit). Want out? git rebase --abort returns everything to how it was.

git rebase <branch>Replay current branch on top of <branch>
git rebase --continueContinue after resolving a conflict
git rebase --abortCancel and restore the original branch
git pull --rebaseUpdate from remote without merge commits

3 Check your understanding

Question 1

After git rebase main, do your feature commits keep their hashes?

Question 2

You are on feature in this graph and run git rebase main. What does the result look like?

b01499b Initialb01499bInitialadb9496 Add searchadb9496Add search55a8344 Fix typo55a8344Fix typoHEAD = where you are. It points at the current branch.HEAD →Branch "feature" — a movable pointer (current branch: moves when you commit)featureBranch "main" — a movable pointermain
Question 3

Which situation breaks the golden rule of rebasing?

Question 4

A rebase stops with a conflict. You fix the file. Next?

4 Practice for real

Your mission

Rebase through a conflict

You are on feature. Rebase it onto main so the history is linear. One commit will conflict on app.txt — resolve it (any content without markers) and finish the rebase.

b01499b Initial click to insert the hash into the terminalb01499bInitialadb9496 Add search click to insert the hash into the terminaladb9496Add search236635a Tweak app click to insert the hash into the terminal236635aTweak appa1e0448 Main tweak click to insert the hash into the terminala1e0448Main tweakHEAD = where you are. It points at the current branch.HEAD →Branch "feature" — a movable pointer (current branch: moves when you commit)featureBranch "main" — a movable pointermain
Working directoryyour folder / editor
app.txt
search.txt
git add →← git restore
Staging areathe next snapshot (index)
app.txt
search.txt
git commit →← restore --staged
RepositoryHEAD → 236635a
app.txt
search.txt
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (feature) $