dev/diff/ GitDiff


Something I found very useful when following these videos was using git diff between consecutive commits. I wrote a simple python script to automate this, which I'll share here in case anybody wants it.

first, run

git log | grep commit > commits

to create a list of commits.

Then make a folder called diffs to put the diffs in. (Add commits and diffs to your .ignore.)

Then the script (gitdiff.py) is:

from subprocess import run, PIPE
commits = open("commits").read().rstrip().splitlines()
commits = list(reversed(commits))
l = len(commits) - 1
c = lambda t: t.split(" ")[1]
for i in range(l):
  a = c(commits[i])
  b = c(commits[i+1])
  with open(f"diffs/commits_{i:02d}_{a[:6]}_{b[:6]}.txt","wt") as f:
    m = run(["git","diff",a,b],stdout=PIPE)
    s = m.stdout.decode()
    print(f"Diff {i}: {a} to {b}")
    print(f"Diff {i}: {a} to {b}",file=f)
    print(s,file=f)

then just run

python gitdiff.py

The result of that is that you'll have a bunch of files in the diffs/ folder you created which you can peruse to see the actual changes from step to step. My intent is to add text notes to these files as I read through them. The beauty of this is that you're no longer constrained to the pace of the videos, which may be slower or faster than your taste, and you don't have the 'I missed that — rewind' problem that you always get with coding walkthrough videos.

Indeed this works with any git repo where there is a single chain of commits. (For anything with more complicated branching structure, you'll need to work out a sequence of commits to break down into steps.)