Ever wondered why developers choose rebase or merge to combine their work without chaos?
Rebase vs merge mental model in Git - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you and your friend are writing a story together, but you both write different chapters separately. Now, you want to combine your chapters into one book.
If you just copy and paste chapters without order, the story might be confusing or have repeated parts. Doing this by hand takes a lot of time and can cause mistakes.
Using rebase or merge in Git helps combine your work smoothly. Merge keeps all chapters as they are but joins them together, while rebase rewrites your chapters to fit perfectly after your friend's, making the story flow better.
copy chapters manually
paste chapters in random ordergit merge feature-branch
or
git rebase mainIt lets teams combine their work clearly and efficiently, avoiding confusion and keeping history clean.
A developer finishes a new feature while others update the main project. Using rebase or merge helps add the feature without breaking the project or losing work.
Manual combining is slow and error-prone.
Merge joins work preserving all history.
Rebase rewrites history for a cleaner story.
Practice
git merge and git rebase?Solution
Step 1: Understand
git mergebehaviorgit mergecombines two branches by creating a new commit that preserves the history of both branches without changing existing commits.Step 2: Understand
git rebasebehaviorgit rebasemoves or reapplies commits from one branch onto another, rewriting history to make it look like a straight line.Final Answer:
git mergecombines histories preserving all commits;git rebaserewrites history to create a linear sequence. -> Option DQuick Check:
Merge preserves history, rebase rewrites it [OK]
- Thinking merge deletes branches
- Believing rebase only works on remote branches
- Confusing which command rewrites history
main?Solution
Step 1: Identify the command to rebase current branch
The commandgit rebase mainrebases the current branch onto themainbranch.Step 2: Check other options for correctness
git merge mainmerges, not rebases;git rebase origin/mainrebases onto remote tracking branch which may be outdated;git checkout main && git rebaseis invalid syntax.Final Answer:
git rebase main -> Option AQuick Check:
Rebase current branch onto main = git rebase main [OK]
- Using merge instead of rebase
- Rebasing onto remote branch without fetching
- Incorrect chaining of commands
feature:
git checkout feature git rebase main git log --oneline --graphWhat will the commit history look like compared to using
git merge main instead?Solution
Step 1: Understand effect of
Rebasing moves feature commits to be based on the latest main commits, creating a straight, linear history.git rebase mainon feature branchStep 2: Compare with
Merging creates a new merge commit that combines histories, preserving the branch structure and showing a branch point.git merge maineffectFinal Answer:
A linear history with feature commits on top of main commits. -> Option AQuick Check:
Rebase = linear history; merge = merge commit [OK]
- Thinking rebase creates merge commits
- Believing history stays unchanged after rebase
- Assuming commits are deleted after rebase
git rebase main on your feature branch but got conflicts. After resolving conflicts, which command should you run to continue the rebase?Solution
Step 1: Identify the correct command to continue rebase after conflicts
After resolving conflicts during a rebase,git rebase --continuetells Git to proceed with applying remaining commits.Step 2: Understand other options
git merge --continueis for merge conflicts, not rebase;git commit -mis manual commit but rebase expects--continue;git rebase --abortcancels the rebase.Final Answer:
git rebase --continue -> Option CQuick Check:
Continue rebase after conflicts = git rebase --continue [OK]
- Using merge commands during rebase
- Trying to commit manually instead of continuing
- Aborting instead of continuing rebase
main but keep a clean, linear history without merge commits. Which sequence of commands achieves this safely?Solution
Step 1: Fetch latest changes from remote main branch
git fetch originupdates local remote tracking branches without changing working branches.Step 2: Rebase feature branch onto updated origin/main
git rebase origin/mainreapplies feature commits on top of latest main commits, keeping history linear and clean.Final Answer:
git checkout feature; git fetch origin; git rebase origin/main -> Option BQuick Check:
Fetch then rebase for clean update [OK]
- Merging instead of rebasing for linear history
- Pulling directly on feature branch causing merge commits
- Not fetching latest remote changes before rebase
