Skip to content
jsworkbench.
Learning path
Intermediate18 min

Rebase compared with merge

Understand the idea

Merge keeps both lines and joins them. Rebase replays one line on top of the other and writes new commits.

The idea

replays your commits onto a new base. Each replay compares the commit with its parent and applies that change on top of the new tip. The result is a new hash. The old commits stay reachable from the reflog. instead adds one commit with two parents and keeps the old hashes.

What changes

Use the player under the terminal. The left graph grows a two-parent commit. The right graph moves topic forward onto a new commit whose snapshot still contains main's README change plus topic's extra file. git rebase -i lets you pick, drop, or reword before that replay. cherry-pick replays one commit the same way, onto whatever HEAD is now.

Merge and rebase on the same start
QuestionMergeRebase
Do old hashes remain the branch tip?YesNo, new hashes are written
How do the lines join?A commit with two parentsA straight line on the new base
What is copied?Nothing; both parents are keptThe change each commit introduced

The command

The player runs the engine one step at a time. In the terminal you can repeat it: commit on main, branch feature, change a different file, git switch feature, then git rebase main. If the same lines overlap, rebase stops with markers and waits for git rebase --continue. Do not rebase commits someone else has already fetched.

The mistake

Check your graph

After rebase, feature points at a new hash whose parent is main's tip. git show on that commit still contains main's file and the file feature added. The old feature hash remains in git reflog.

Your repository

Use the side-by-side player. The same start becomes two different graphs.

  1. Step 1

    Branch before main moves

    feature starts here. If it already exists, run git switch feature.

  2. Step 2

    Add the feature file

    Create extra.txt in the editor and set the text to: feature notes

  3. Step 3

    Stage the feature file

    The editor change is unstaged until this command.

  4. Step 4

    Commit the feature

    feature now has a commit main does not have yet.

  5. Step 5

    Move main forward

    Switch to main. In the editor set README.md to include the line: Updated on main

  6. Step 6

    Stage main’s newer line

    Add the README edit before you commit it.

  7. Step 7

    Commit main’s newer line

    main is now ahead of where feature started.

  8. Step 8

    Return to the feature

    Rebase replays the branch you have checked out.

  9. Step 9

    Replay the feature

    The feature change is applied onto main’s newer README. The feature commit gets a new hash.

  10. Step 10

    Compare the graphs

    Use the player under this practice, then read your own graph.

The editor edits the working tree. Start a repository and the files appear here.

Follow the steps. Each command step can be placed in the terminal. Edit files in the editor, then stage them.
Supported command reference
  • init
  • add <path> | add .
  • commit -m "message" | commit --amend -m "message"
  • status
  • diff | diff --staged
  • log | log --oneline | log --graph | log --oneline --graph
  • show [ref]
  • branch | branch <name> | branch -d <name>
  • checkout <ref> | checkout -b <name> | switch <name> | switch -c <name>
  • merge <branch>
  • rebase <branch> | rebase -i <branch> | rebase --edit-todo <pick|drop|reword> <hash> [message] | rebase --continue | rebase --abort
  • cherry-pick <ref>
  • reset --soft <ref> | reset --mixed <ref> | reset --hard <ref>
  • revert <ref>
  • stash | stash list | stash pop | stash apply
  • restore <path> | restore --staged <path>
  • remote | remote add <name> <repo-slug>
  • clone <repo-slug> | git clone <repo-slug> | git fork <repo-slug>
  • fetch [remote] | pull [remote] [branch] | push [remote] [branch] | push --force [remote] [branch]
  • blame <path>
  • reflog
  • tag | tag <name> [ref] | tag -d <name>
  • bisect start | bisect good [ref] | bisect bad [ref] | bisect reset
  • config <key> <value> | config --list
  • write <path> <text> (workbench: edit the working tree)
  • issue new "title" | pr new <source> <target> "title" | review "comment" | review --request-changes "comment"

Commit graph

No commits yet. git init creates the first one.

Both sides start from the same two commits that edited README.md.

main and topic each point at their own snapshot.

Merge

main edit622a2be main HEADtopic edit4bd4ad2 topicstart757cd88Initial commit9ecf946

Rebase

main edit622a2be main HEADtopic edit4bd4ad2 topicstart757cd88Initial commit9ecf946

Quiz

A quick check for understanding. Retry as often as you like; your learning path stays open.

01What does rebase change?
02Which history should you avoid rebasing?

Ready to call this one understood?

Mark this lesson complete to earn 150 XP. You can always revisit it.

Sign in to save progress
Next lesson