Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Investigating History

    A detective's toolkit: compare two photos side by side; ask "who last changed this sentence?"; and, when you know the

    Chapter 11 of 113 min read

    The Plain Picture#

    A detective's toolkit: compare two photos side by side; ask "who last changed this sentence?"; and, when you know the album was fine on page 10 and broken on page 200, check the middle page, then the middle of the remaining half, and so on.

    The Real Names and Commands#

    Terminal
    git diff                          # unstaged changes vs the last commit
    git diff --staged                 # staged changes (what will be committed)
    git diff main..feature            # everything that differs between two branches
    git log --oneline --graph --all   # the whole history as a graph (as in Ch 5)
    git log -p quiz.py                # every change ever made to one file, with diffs
    git log --author="Rahul" --since="2 weeks ago"
    git show 640a7d3                  # one commit: message + diff
    git blame quiz.py                 # for each line: which commit and author last changed it
    git cherry-pick 9ffbfe2           # copy ONE commit from another branch onto the current branch
    git tag v1.4.0                    # a permanent name for a commit — a release
    git push origin v1.4.0

    git bisect — binary search for the bug#

    Part 0's problem 3: scores are wrong now; they were right at release v1.3.0, 200 commits ago. Instead of checking 200 commits, Git halves the range each time — about 8 checks (2⁸ = 256 ≥ 200; this is binary search, which you'll learn properly in DSA).

    Terminal
    git bisect start
    git bisect bad                    # the current commit is broken
    git bisect good v1.3.0            # this one was fine
    # Git checks out a commit halfway between. Test it, then tell Git:
    git bisect good                   # or: git bisect bad
    # ...repeat ~8 times until Git prints: "<hash> is the first bad commit"
    git bisect reset                  # return to where you started

    If you have a script that exits 0 when things work and non-zero when they don't (The Linux Command Line Ch 9), git bisect run ./test_scores.sh does all the steps automatically.

    Check yourself

    4 questions
    1

    How do you see what you're about to commit?

    Show answer

    git diff --staged.

    2

    How do you find who last changed line 42 of quiz.py?

    Show answer

    git blame quiz.py (or git blame -L 42,42 quiz.py).

    3

    A bug appeared somewhere in the last 1,000 commits. About how many tests will git bisect need?

    Show answer

    About 10 (2¹⁰ = 1,024).

    4

    What does cherry-pick do?

    Show answer

    Copies a single commit from elsewhere onto the current branch.