Interactive Rebasing & History Rewriting (The Masterclass Manual)

An analogy-driven, professional-grade guide to Git interactive rebasing, history rewriting, commit squashing, Reflog mechanics, and Git reset modes (soft, mixed, hard).

High-Level Concept Definition & Real-World Analogy

Rebasing and History Rewriting represent Git's capabilities for modifying, combining, reordering, or re-parenting commit nodes within the DAG.

Core Architectural Features

  • Linear History Creation: git rebase replays commits onto the tip of another branch, eliminating unnecessary 2-parent merge commits.
  • Commit Mutation: Every replayed commit receives a new parent SHA-1, producing a brand new object key.
  • Interactive Refactoring: git rebase -i permits squashing WIP commits, rewording logs, and dropping unwanted commits.

Real-World Analogy: The Movie Film Editing & Snipping Room

To visualize rebasing and history rewriting, consider a Movie Film Editing and Snipping Room:

  • Original Commit Graph: Raw camera footage containing out-of-order scenes, microphone mishaps, and false starts.
  • git rebase main: Snipping the Film Strip. Snipping a feature scene strip off an old scene 10 roll and splicing it onto the end of scene 45 for continuous play.
  • git rebase -i (Interactive Rebase): The Editing Light Table. Arranging film frames to merge takes (squash), edit lines (reword), or discard scenes (drop).
  • Git Reset (--soft, --mixed, --hard): Rewinding the Film Projector Deck:
    • --soft: Rewinds projector 3 scenes; actors stay in costume (Index & Working Directory preserved).
    • --mixed: Rewinds projector and resets actor costumes (Index cleared; Working Directory preserved).
    • --hard: Rewinds projector, clears stage, and wipes script notes (Index & Working Directory reset).
  • Git Reflog: The CCTV Recording of the Editing Room Floor. Records every floor location so discarded film strips can be recovered.
Original Topology [Original Diverged DAG] Rebased Topology [Linear Rebased Topology] Commit C1 Commit C2 (main) Commit C3 (feature) Commit C4 (feature) Commit C1 Commit C2 (main) Commit C3' (New SHA!) Commit C4' (New SHA! HEAD -> feature)

Structured Module Roadmap

ModuleCore TopicsKey Focus & Engineering ConceptsRead Time
Rebase MechanicsReplay Engine, SHA Hash MutationLinearization, Patch Applicator, Re-parenting Commit Nodes5 min
Interactive Rebase Toolkitrebase -i, pick, squash, fixup, dropCleaning WIP Commits, Reword, Edit, Atomic PR Structuring5 min
Reset Modes Deep-Dive--soft, --mixed, --hardHEAD Pointer Movement vs. Index vs. Working Directory State5 min
Reflog & Emergency Safety Netgit reflog, git reset --hard HEAD@{n}Recovering Deleted Commits, Reference Transaction Logs5 min

Quick Reference & Comparison Matrices

1. Git Reset Modes State Taxonomy Matrix

Reset Command ParameterHEAD Pointer Moved?Index (Staging Area) Updated?Working Directory Files Modified?Primary Production Use Case
git reset --soft HEAD~1Yes (Moved back)No (Staged changes remain)No (Workspace untouched)Re-committing changes with modified commit messages.
git reset --mixed HEAD~1 (Default)Yes (Moved back)Yes (Un-stages changes)No (Workspace untouched)Un-staging files to selectively re-stage granular commits.
git reset --hard HEAD~1Yes (Moved back)Yes (Cleared)YES (Overwritten to target commit)Discarding uncommitted local experiments completely.

2. Interactive Rebase Command Glossary Matrix

Rebase Action CodeShort CodeOperational MeaningDAG Impact
pickpRetain and apply commit as-isRe-applies commit with a new parent SHA
rewordrRetain commit content, edit log messageRe-applies commit with modified commit message string
editePause rebase loop at this commit for editingHalts rebase; allows code editing or splitting
squashsCombine commit into previous commit; merge messagesMerges code with previous commit; prompts for combined message
fixupfCombine commit into previous commit; discard messageMerges code into previous commit; discards current message log
dropdRemove commit completely from historyDeletes commit node from replayed DAG sequence

CLI Command Masterclass: History Rewriting & Resetting

1. git rebase & git cherry-pick

  • Mental Model: git rebase un-hooks a sequence of commits and re-applies them onto a new parent. git cherry-pick copies a single commit patch from anywhere in the DAG onto the current branch tip.
# 1. Rebase current feature branch onto updated main
$ git rebase main

# 2. Interactive rebase over last 3 commits
$ git rebase -i HEAD~3

# 3. Abort a paused rebase safely
$ git rebase --abort

# 4. Cherry-pick a specific hotfix commit by SHA
$ git cherry-pick c108a9f

2. git reset & git reflog

  • Mental Model: git reset moves the branch pointer backwards in time. git reflog acts as a transaction log of all pointer movements.
# 1. Soft Reset: Move HEAD back 1 commit, keeping changes staged
$ git reset --soft HEAD~1

# 2. Hard Reset: Move HEAD back 1 commit, wiping all unstaged edits (CAUTION!)
$ git reset --hard HEAD~1

# 3. Inspect Reference Transaction Log
$ git reflog

# 4. Recover lost commit after hard reset
$ git reset --hard HEAD@{1}

Architectural Deep-Dive & Engineering Concepts

The Golden Rule of Rebasing

Golden Rule of Rebasing:
Never rebase commits that have already been pushed to a shared public remote branch.
git rebase generates new SHA-1 hashes for replayed commits. Rebasing public commits forces team members' local DAGs out of sync, causing duplicate commit chains and complex merge conflicts upon pulling.

Emergency Commit Recovery via Reflog

If git reset --hard accidentally discards commits, objects remain stored temporarily. Git logs all pointer movements in .git/logs/HEAD (Reflog):

# 1. Inspect Reference Transaction Log
$ git reflog
7a8b9c0 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~5
1088abc HEAD@{1}: commit: Implement real-time notifications
4410def HEAD@{2}: commit: Add database indexing strategy

# 2. Rescue lost commit by resetting HEAD to its Reflog position!
$ git reset --hard HEAD@{1}
HEAD is now at 1088abc Implement real-time notifications

Interactive Self-Assessment Checkpoints

Knowledge Check

Why does executing git rebase main on a feature branch produce new commit SHA hashes even if the file code content is identical to before?

Knowledge Check

What is the precise functional difference between git reset --soft HEAD~1 and git reset --hard HEAD~1?

Knowledge Check

How does git reflog allow developers to recover commits that were accidentally lost after a git reset --hard?

Problem: Squashing Messy Feature Branch Commits Before Pull Request

You have 3 WIP commits on your local branch feature/cart:

  • a1b2c3d: "WIP cart feature"
  • e5f6g7h: "fix bug"
  • i9j0k1l: "done cart"

Squash these 3 commits into 1 single clean atomic commit titled "Feature: Add shopping cart checkout pipeline" using interactive rebase.

On this page