Log inStart free
LESSON 1.18 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Merge vs rebase

Watch two branches join: one merge commit, or rebased copies with new hashes.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 1.18 · GIT BAYFEATURE: 2 AHEAD · 1 BEHIND
01
Stage 1 of 6 · Two branches
Each commit stores a full snapshot. The branch feature is 2 commits ahead of main and 1 behind.
Explain it simply

Each box is a commit, a saved copy of all the files. The table shows the files in each one. Feature added X and Y, and then main added B, which changes data.py. So feature has 2 commits that main doesn't have, and main has 1 that feature doesn't.

Commit A starts both branches. Feature adds X and Y. Main adds B, which changes data.py. Feature is 2 ahead and 1 behind.

commit, file linehash, parent link, branchnew commit, resultconflict, left behindno file, not written
historysnapshots$ git rev-list --left-right --count main...feature1 2A726d4afStartXb18011cTune lrYdaaff67Add evalB944e462Raise batchHEAD → mainfeaturedata.pyeval.pytrain.pysnapshotA 726d4aftree ea1ff87batch = 32no filelr = 0.1snapshotX b18011ctree ec6c9dbbatch = 32no filelr = 0.01snapshotY daaff67tree 1976d37batch = 32print("eval")lr = 0.01snapshotB 944e462tree 96bbfbbbatch = 64no filelr = 0.1historysnapshots$ git rev-list --left-right --count main...feature1 2A726d4afStartXb18011cTune lrYdaaff67Add evalB944e462Raise batchHEAD → mainfeaturedata.pyeval.pytrain.pysnapshotA 726d4aftree ea1ff87batch = 32no filelr = 0.1snapshotX b18011ctree ec6c9dbbatch = 32no filelr = 0.01snapshotY daaff67tree 1976d37batch = 32print("eval")lr = 0.01snapshotB 944e462tree 96bbfbbbatch = 64no filelr = 0.1
Toy: 3 one-line files · 4 commits, then a merge or a rebase · real Git SHA-1 ids
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
ahead=∣feature∖main∣,behind=∣main∖feature∣\text{ahead} = |\sy{ahead}{data}{\text{feature} \setminus \text{main}}|,\quad \text{behind} = |\sy{behind}{data}{\text{main} \setminus \text{feature}}|
ahead=∣{X, Y}∣=2,behind=∣{B}∣=1\text{ahead} = |\{X,\ Y\}| = 2,\quad \text{behind} = |\{B\}| = 1

Ahead counts commits only feature has; behind counts commits only main has.

Common mix-ups

MYTHRebase moves the feature commits onto main.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01After a rebase, does the copy X′ keep the hash of X?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.