🎓 A1Learner
🔍
0 of 50 lessons completed

Rebase a Branch

Rebase moves your snapshots onto a newer base. It replays them one by one, so the history reads as a straight line.

See the split

The tidy branch grew from an older base. Main moved on since then.

$ git log --oneline --graph --all

Replay the work

One snapshot is replayed onto the newer base.

$ git rebase main

The line is straight now

The oats snapshot sits on top of the Sunday park snapshot.

$ git log --oneline --graph

Example

$ git rebase main
$ git log --oneline --graph

Key points

  • Rebase with a branch name replays your work on top of it.
  • Each snapshot is copied onto the new base.
  • The copies get new hashes. The old ones are gone.
  • The history reads as a straight line afterwards.
  • Rebase only your own branches, never shared ones.
  • After a rebase, a merge fast-forwards.

Here, rebase copies the oats snapshot onto the newer base. The copy has a new hash, f3ba1ca, and the history reads straight.

Note

Rebase rewrites history. Never rebase work others already have.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does rebase do?