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.
| Question | Merge | Rebase |
|---|---|---|
| Do old hashes remain the branch tip? | Yes | No, new hashes are written |
| How do the lines join? | A commit with two parents | A straight line on the new base |
| What is copied? | Nothing; both parents are kept | The 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.
Step 1
Branch before main moves
feature starts here. If it already exists, run git switch feature.
Step 2
Add the feature file
Create extra.txt in the editor and set the text to: feature notes
Step 3
Stage the feature file
The editor change is unstaged until this command.
Step 4
Commit the feature
feature now has a commit main does not have yet.
Step 5
Move main forward
Switch to main. In the editor set README.md to include the line: Updated on main
Step 6
Stage main’s newer line
Add the README edit before you commit it.
Step 7
Commit main’s newer line
main is now ahead of where feature started.
Step 8
Return to the feature
Rebase replays the branch you have checked out.
Step 9
Replay the feature
The feature change is applied onto main’s newer README. The feature commit gets a new hash.
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.
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
Rebase
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.