Skip to content

05. Diffs

ivomac edited this page Mar 16, 2025 · 1 revision

πŸ” Diffs

GitObject = Commit | Branch | Index | WorkDir
def diff(a: GitObject, b: GitObject, files: list[Path]):
  • A diff shows changes between two versions of one or multiple files.
  • You can compare any file pair at any point in the graph:
    main      dev β—„ HEAD + πš’πš 
     β–Ό         β–Ό
A ◄─ B ◄─ C ◄─ D
          β–²
          E ◄─ F
               β–²
             bugfix
  • For example, we could compare:
    • πŸ—οΈ The workdir (𝚠) with the index (πš’).
    • ⏳ A file in the workdir (𝚠) with that same file in main.
    • πŸ”„ D with F, or equivalently, dev with bugfix
  • πŸ§™ A diff has all the information needed to turn one version into the other.

🧩 Hunks

  • A hunk is a section of text containing changes within a file.
    • πŸ“œ Part of the hunk serves only as context.
  • Git uses a line-by-line comparison to identify what has changed:
--- a/kiwi.py
+++ b/kiwi.py
@@ -20,5 +22,5 @@
     total = 0
     for kiwi in kiwis:
-        total += kiwi["price"]
+        total += kiwi["price"] * (1 - kiwi["discount"])
     return total
@@ -43,2 +44,6 @@
 
+
+# Todo list:
+# - Retrieve prices/discounts from supermarket websites
+# - Setup alerts for big kiwi discounts
 
  • Diff header shows we are comparing two versions of kiwi.py.
  • There are two hunks in the diff, with hunk headers @@ -x,n +y,m @@:
    • -/+ refer to the a/b versions of the file.
    • x/y refer to the line number of the first line of the hunk in the a/b versions.
      • In this case, total = 0 is at line 20 in a and line 22 in b.
    • n/m refer to number of lines in the hunk in each version.

🌲 Compare Versions

  • Hold Ctrl and select both commits/branches.

  • Right-click and select diff against each other (or use Diff panel at the bottom).

  • Workdir Changes: Compares the state at the index (πš’) with the workdir (𝚠).

  • Staged Changes: Compares the HEAD with the index (πš’).

  • History View: Shows diffs between a commit and its parent.

🌲 Diff Tools

  • SourceTree integrates with popular diff tools like:

πŸ–₯️ Set VS Code as SourceTree diff tool

  1. Go to Tools β†’ Options β†’ Diff.
  2. Under External Diff / Merge, set:
    • Diff Tool: Custom
    • Merge Tool: Custom
  3. In the Diff Command field, enter the path to VS Code executable:
"C:\Program Files\Microsoft VS Code\Code.exe" # or similar
  1. In Arguments, enter:
    • Diff: --diff "$LOCAL" "$REMOTE"
    • Merge: -n "$MERGED"

πŸ“ Commit Distance

  • πŸ›€οΈ To more quickly understand the state of a repository, it is common to report the "distance" of a branch to another reference branch.
  • ⬆️ If a branch is a successor of the reference, then the distance is how many commits ahead the branch is:
    • (⬆️1) means the branch is 1 commit ahead (is a child).
    • Example: bugfix is 3 commits ahead of main: (⬆️3).
    • Another interpretation is that we must advance 3 commits from main to reach bugfix.
    main      dev β—„ HEAD + πš’πš 
     β–Ό         β–Ό
A ◄─ B ◄─ C ◄─ D
          β–²
          E ◄─ F
               β–²
             bugfix
  • ⬇️ Similarly if a branch is an ancestor of the reference: main is ⬇️3 behind bugfix.
  • ↕️ If the branches being compared do not share a direct history, we count steps both ways:
    • Example: bugfix is 1 commit behind, 2 commits ahead of dev: (⬇️1⬆️2).
    • Or, from dev to bugfix, we go back one commit and go forward 2 commits.

Clone this wiki locally