Unit 6 of 8 · about 4 hours · explain, implement

Git history, merges, rebases, bisect and recovery

History is the experiment log of the code. Bisect finds which change broke behavior, and reflog makes almost every Git mistake reversible.

You'll be able to

  • Explain commits, branches, merges, rebases and detached state using the snapshot graph.
  • Isolate a regression with bisect run from an automated failing test.
  • Recover a displaced commit with reflog without discarding unrelated work.

Covers

Commits, branches, merges, rebases and detached stateBisect, reflog and preservation-first recovery
1

Read

In this order, only the parts listed

0 of 6 done

  1. Version control and Git data modelThe Missing Semester of Your CS Education

    Git data model: Snapshots, Modeling history, Objects and content-addressing, References; Staging area; Basics, Branching and merging; Advanced Git; the recipe.txt merge-conflict exercise

  2. Pro Git: basic branching and mergingPro Git

    Basic Branching; Basic Merging; Basic Merge Conflicts

  3. Pro Git: rebasingPro Git

    The basic rebase flow; More Interesting Rebases with --onto; The Perils of Rebasing; Rebase vs. Merge

  4. Pro Git: debugging with GitPro Git

    bisect start, bad, good and run with test-error.sh; blame -L for line-level attribution

  5. Pro Git: undoing thingsPro Git

    Undoing Things: Unstaging a Staged File; Unmodifying a Modified File; Undoing things with git restore

  6. Pro Git: data recoveryPro Git

    Maintenance and Data Recovery: Data Recovery (reflog, git log -g, fsck --full for dangling commits)

2

Focus on

  • A commit is a snapshot plus parents, and a branch is a movable pointer. Merge preserves both parents; rebase rewrites commits onto a new base.
  • Rebase only local, unpushed work. Rebasing pushed history forces everyone downstream to repair, which the book states as the rule.
  • Bisect needs a monotonic test: nonzero exit on bad, zero on good, plus one known-good and one known-bad commit. git bisect run automates the search in logarithmic steps.
  • Recovery is preservation-first: stash or branch current state before any destructive command. Uncommitted work is invisible to reflog, and unreachable entries expire after 30 days by default.
Common mistakes · 3
  • Destructive commands before saving state. Uncommitted work never reaches the reflog, so stash or commit before reset --hard or clean.
  • Bisecting with a flaky or non-monotonic test. The search converges on the wrong commit, so stabilize the test first.
  • Rebasing a shared branch then force-pushing. Use merge, or rebase only with coordination, and never rewrite pushed history silently.
3

Try it yourself

Closed-book, before checking anything

Build a 7-commit repo with a regression introduced in commit 6 and a test.sh that exits 1 on the bad line. Run git bisect start, bad, good HEAD~6 and run ./test.sh: it tests commits 4, 6 and 5 and reports commit 6 as the first bad commit. Then delete a branch tip and recover it with git reflog plus git branch recovered <sha>. Observed with git 2.51.0.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Merge versus rebase for a finished feature branch: what differs in the graph?Answer

    Merge adds a merge commit with two parents and preserves branch history. Rebase replays the branch commits onto the main tip as new hashes with a linear history.

  2. 2.git bisect run reports the wrong culprit. Name two causes.Answer

    A flaky non-monotonic test, or a bad and good labeling error. Uncommitted changes skewing some checkouts is a third.

  3. 3.You deleted a branch yesterday and need its tip. What is the exact recovery?Answer

    git reflog, find the tip sha, then git branch recovered <sha>. It works while the reflog entry survives expiry.

  4. 4.In detached HEAD state, what does committing do, and what is the risk?Answer

    The commit exists but no branch points at it. Switching away strands it except in the reflog, so create a branch to keep it.

5

Practice

You're done when

Your bisect finds the seeded culprit without manual checkouts, and your recovered branch matches the displaced tip sha.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 5 more in the section
  • The Missing Semester of Your CS EducationMIT · primary

    Complete the 2026 lectures on the shell, command-line environment, development tools, debugging and profiling, Git, packaging and shipping code, and code quality. Apply every exercise to the same stage project.

  • Pro GitGit project · support

    Use Chapters 2-3 and the sections on debugging, internals and recovery while constructing a history for bisect and recovering a deliberately displaced commit.

Also in this section

  • pytest documentationpytest · required

    Use Get Started, fixtures, parametrization, monkeypatch, caplog and temporary paths to build behavior and failure-path tests without order dependence.

  • Hypothesis documentationHypothesis · required

    Follow the Quick start and property-based testing guides, then generate operation histories and retain the minimized example for one real defect.

  • The Art of DebuggingStas Bekman · support

    Use the Unix, Python and compiled-code sections during a real seeded defect. Follow the evidence and tiny-reproducer workflow rather than reading the unfinished book front to back.

  • Stas' Python Cookbook - debugging and testingStas Bekman · support

    Use Part III and the packaging sections for debugger, traceback, pytest, logging, profiling and clean-environment recipes.

  • AlgoMaster Python - mockingAlgoMaster · support

    Use the Mocking lesson for unittest.mock: Mock versus MagicMock, patch targets, autospec and the over-mocking trap. The pytest docs remain the reference for fixtures and monkeypatch.