Rebasing Branches: Linear History with git rebase vs git merge0%
Tagging Milestones and Software Releases with git tag

Rebasing Branches: Linear History with git rebase vs git merge

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Rebasing Branches: Linear History with git rebase vs git merge

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.


1. The Core Concept: Re-basing Your Commits

Consider two diverging branches:

Output
Base Branch (main): (C1) ---> (C2) -------------> (C3) ---> (C4) [main]
\
Feature Branch: (C5) ---> (C6) [feature-search]

If You Use git merge:

Merging creates a merge commit tying C4 and C6 together, preserving the branching knot.

If You Use git rebase:

"Rebase" literally means: take the base of my branch and change it to a new base commit! Git:

  1. 1
    Temporarily shelves commits C5 and C6 as diff patches.
  2. 2
    Fast-forwards feature-search to the tip of main (C4).
  3. 3
    Re-applies the patches C5 and C6 one by one on top of C4 as brand new commits (C5' and C6' with new hashes!).
Output
After `git rebase main`:
(C1) ---> (C2) ---> (C3) ---> (C4) [main] ---> (C5') ---> (C6') [feature-search]

The resulting history is completely flat and linear, as if you started developing your feature today!


2. How to Rebase in Practice

Bash / Terminal
# 1. Switch to the feature branch you want to rebase
git switch feature-search
 
# 2. Rebase onto main
git rebase main

Resolving Conflicts During Rebase:

If a conflict occurs while applying C5':

  1. 1
    Git halts and displays the conflict markers in the file.
  2. 2
    Edit the file, remove the conflict markers, and stage: git add <file>.
  3. 3
    > [!IMPORTANT]

Do NOT run git commit! Instead, continue the rebase:

Bash / Terminal
git rebase --continue
  1. 1
    If you want to abort the rebase and return to where you started:
Bash / Terminal
git rebase --abort

3. Interactive Rebasing: Polishing Commits (git rebase -i)

Before submitting a Pull Request, you might have several messy commits:

  • "wip"
  • "fixed typo"
  • "forgot semicolon"
  • "actual feature"

Interactive rebase lets you clean, reorder, and squash them into professional commits:

Bash / Terminal
# Interactively rebase the last 4 commits
git rebase -i HEAD~4

Git opens an editor showing:

Output
pick 3a89e12 feat: add search input component
pick 4b91e23 wip: test debounce
pick 7c10d34 fix typo in search handler
pick 9f20e45 style: align search dropdown

Common Interactive Commands:

  • 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!

4. The Golden Rule of Rebasing

Important Warning
[!CAUTION] The Golden Rule of Rebase: Never rebase a branch that has been pushed to a public, shared repository (like 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.

Interactive Quiz

Practice Quiz

Question 1
What is the primary visual difference between a history merged with git merge vs git rebase?
Question 2
What happens to the commit hashes of commits that are rebased?
Question 3
During an active rebase with conflicts, after resolving conflict markers and staging files, what command should you run?
Question 4
What does the squash (or s) command do inside an interactive rebase (git rebase -i)?
Question 5
Why must developers never rebase public shared branches like main?
Next Lesson

Tagging Milestones and Software Releases with git tag

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext