-
Notifications
You must be signed in to change notification settings - Fork 0
05. Diffs
ivomac edited this page Mar 16, 2025
·
1 revision
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.
- 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/yrefer to the line number of the first line of the hunk in the a/b versions.- In this case,
total = 0is at line 20 in a and line 22 in b.
- In this case,
-
n/mrefer to number of lines in the hunk in each version.
-
-
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.
- Go to Tools β Options β Diff.
- Under External Diff / Merge, set:
- Diff Tool: Custom
- Merge Tool: Custom
- In the Diff Command field, enter the path to VS Code executable:
"C:\Program Files\Microsoft VS Code\Code.exe" # or similar
- In Arguments, enter:
-
Diff:
--diff "$LOCAL" "$REMOTE" -
Merge:
-n "$MERGED"
-
Diff:
- π€οΈ 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.