In team development, branches inevitably diverge. While git merge combines branches by generating a two-parent merge commit, Git provides an alternative approach called git rebase. Rebasing rewrites history to create a clean, completely linear commit timeline.
Consider two diverging branches:
git merge:Merging creates a merge commit tying C4 and C6 together, preserving the branching knot.
git rebase:"Rebase" literally means: take the base of my branch and change it to a new base commit! Git:
C5 and C6 as diff patches.feature-search to the tip of main (C4).C5 and C6 one by one on top of C4 as brand new commits (C5' and C6' with new hashes!).The resulting history is completely flat and linear, as if you started developing your feature today!
If a conflict occurs while applying C5':
git add <file>.Do NOT run git commit! Instead, continue the rebase:
git rebase -i)Before submitting a Pull Request, you might have several messy commits:
Interactive rebase lets you clean, reorder, and squash them into professional commits:
Git opens an editor showing:
pick: Keep the commit as is.reword: Keep the commit, but edit the commit message.edit: Pause to amend files within this commit.squash (or s): Melds this commit into the previous commit, combining messages!drop (or d): Completely deletes the commit!main)!
Rebasing changes commit hashes. If teammates have branched off those commits, rebasing creates duplicate conflicting histories that cause chaos. Only rebase your own private local feature branches.git merge vs git rebase?squash (or s) command do inside an interactive rebase (git rebase -i)?main?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.