By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(For Engineers Who Need Clean History, Not Confusion)
Rebasing is rewriting Git history—not just moving commits, but editing them. Think of it like a time machine for your branch: - Bad: You merge a feature branch with 20 messy "WIP" commits. Your main branch now looks like a developer’s diary.- Good: You rebase to squash those 20 commits into 3 clean, logical ones. main stays readable, bisectable, and professional.
main
Why this matters in production:1. Clean history = easier debugging. When git bisect runs, you want one commit that broke things, not 15 "fix typo" commits.2. Team workflows break without it. If you git merge a branch with 50 micro-commits, your PR becomes un-reviewable. Rebasing lets you squash those into a single, coherent change.3. Open-source contributions demand it. Projects like Kubernetes or React require rebased PRs. If you don’t rebase, maintainers will reject your work.
git bisect
git merge
Real-world scenario:You’re a DevOps engineer working on a critical security patch. Your branch has: - 5 "fix typo" commits - 3 "revert previous" commits - 2 "WIP" commits Your team lead says: "Squash this into one commit before merging." You have 10 minutes. Rebasing is your only option.
git rebase
-i
fixup
master
git rebase --abort
--onto
git rebase --onto main feature-bugfix
git reflog
git reset --hard HEAD@{n}
git commit
git checkout
git log
Scenario:You’re working on feature/login. Your branch has: 1. feat: add login form (good) 2. fix: typo in button text (should be squashed) 3. WIP: login validation (needs a better message) 4. fix: missing semicolon (should be a fixup)
feature/login
feat: add login form
fix: typo in button text
WIP: login validation
fix: missing semicolon
Goal: Squash commits 2–4 into commit 1, with a clean message.
git checkout feature/login git log --oneline
Output:
a1b2c3d (HEAD -> feature/login) fix: missing semicolon e4f5g6h WIP: login validation i7j8k9l fix: typo in button text d0e1f2g feat: add login form
git rebase -i d0e1f2g # Rebase onto the commit *before* your first messy commit
Alternative (rebase last 3 commits):
git rebase -i HEAD~3
Git opens an editor (Vim/ Nano) with:
pick i7j8k9l fix: typo in button text pick e4f5g6h WIP: login validation pick a1b2c3d fix: missing semicolon
Change pick to: - squash (or s) to combine commits and edit the message.- fixup (or f) to combine but discard the commit message.- reword (or r) to edit a commit message.- drop (or d) to remove a commit.
pick
squash
s
f
reword
r
drop
d
Your edits:
pick d0e1f2g feat: add login form squash i7j8k9l fix: typo in button text reword e4f5g6h WIP: login validation fixup a1b2c3d fix: missing semicolon
bash # Fix conflicts in the files, then: git add . git rebase --continue
bash git rebase --abort # Undo everything
git log --oneline
a1b2c3d (HEAD -> feature/login) feat: add login form e4f5g6h feat: add login validation
Success! Your 4 commits are now 2 clean ones.
git push --force-with-lease # Safer than --force (checks for upstream changes)
⚠️ Warning: Only force-push to your own branches. Never force-push to main or shared branches.
--force-with-lease
--force
git rebase main
git log --graph
git status
rebase --continue
git fetch --all
Exam trap: "Rebase is safer than merge" → False. Rebasing rewrites history; merging doesn’t.
"How do you squash the last 3 commits?"
git rebase -i HEAD~3 → Change pick to squash for the last 2 commits.
"What does git rebase --onto do?"
git rebase --onto
Moves a branch to a new base. Example: bash git rebase --onto new-base old-base feature-branch
bash git rebase --onto new-base old-base feature-branch
"When should you not rebase?"
develop
Challenge:You have a branch feature/payment with: 1. feat: add payment form 2. fix: typo in button 3. fix: missing semicolon 4. feat: add validation
feature/payment
feat: add payment form
fix: typo in button
feat: add validation
Task:- Squash commits 2 and 3 into commit 1.- Reword commit 4 to feat: add payment validation.- Push the cleaned-up branch.
feat: add payment validation
Solution:
git checkout feature/payment git rebase -i HEAD~4 # Edit to: # pick <commit1> feat: add payment form # squash <commit2> fix: typo in button # fixup <commit3> fix: missing semicolon # reword <commit4> feat: add validation git push --force-with-lease
HEAD~3
git rebase --onto new-base old-base branch
branch
new-base
--onto <new> <old> <branch>
git rebase --continue
git add
git push --force-with-lease
Rebase early, rebase often. The longer you wait, the harder it gets. Run git rebase main every morning—it’s like flossing for your Git history. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.