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 rebasereplays 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 -ipermits 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 Directorypreserved).--mixed: Rewinds projector and resets actor costumes (Indexcleared;Working Directorypreserved).--hard: Rewinds projector, clears stage, and wipes script notes (Index&Working Directoryreset).
- Git Reflog: The CCTV Recording of the Editing Room Floor. Records every floor location so discarded film strips can be recovered.
Structured Module Roadmap
| Module | Core Topics | Key Focus & Engineering Concepts | Read Time |
|---|---|---|---|
| Rebase Mechanics | Replay Engine, SHA Hash Mutation | Linearization, Patch Applicator, Re-parenting Commit Nodes | 5 min |
| Interactive Rebase Toolkit | rebase -i, pick, squash, fixup, drop | Cleaning WIP Commits, Reword, Edit, Atomic PR Structuring | 5 min |
| Reset Modes Deep-Dive | --soft, --mixed, --hard | HEAD Pointer Movement vs. Index vs. Working Directory State | 5 min |
| Reflog & Emergency Safety Net | git reflog, git reset --hard HEAD@{n} | Recovering Deleted Commits, Reference Transaction Logs | 5 min |
Quick Reference & Comparison Matrices
1. Git Reset Modes State Taxonomy Matrix
| Reset Command Parameter | HEAD Pointer Moved? | Index (Staging Area) Updated? | Working Directory Files Modified? | Primary Production Use Case |
|---|---|---|---|---|
git reset --soft HEAD~1 | Yes (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~1 | Yes (Moved back) | Yes (Cleared) | YES (Overwritten to target commit) | Discarding uncommitted local experiments completely. |
2. Interactive Rebase Command Glossary Matrix
| Rebase Action Code | Short Code | Operational Meaning | DAG Impact |
|---|---|---|---|
pick | p | Retain and apply commit as-is | Re-applies commit with a new parent SHA |
reword | r | Retain commit content, edit log message | Re-applies commit with modified commit message string |
edit | e | Pause rebase loop at this commit for editing | Halts rebase; allows code editing or splitting |
squash | s | Combine commit into previous commit; merge messages | Merges code with previous commit; prompts for combined message |
fixup | f | Combine commit into previous commit; discard message | Merges code into previous commit; discards current message log |
drop | d | Remove commit completely from history | Deletes commit node from replayed DAG sequence |
CLI Command Masterclass: History Rewriting & Resetting
1. git rebase & git cherry-pick
- Mental Model:
git rebaseun-hooks a sequence of commits and re-applies them onto a new parent.git cherry-pickcopies 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 c108a9f2. git reset & git reflog
- Mental Model:
git resetmoves the branch pointer backwards in time.git reflogacts 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 notificationsInteractive Self-Assessment Checkpoints
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?
What is the precise functional difference between git reset --soft HEAD~1 and git reset --hard HEAD~1?
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.
Branching, Merging & Strategy Mechanics (The Masterclass Manual)
An analogy-driven, professional-grade guide to Git branching, fast-forward merges, 3-way merges, common ancestor calculations, ORT merge engine architecture, and merge conflict resolution.
Production Troubleshooting, Bisect & Recovery (The Masterclass Manual)
An analogy-driven, professional-grade guide to production Git troubleshooting, automated binary search bug isolation via git bisect, multi-checkout workflows using git worktree, detached HEAD recovery, and repository garbage collection.