Video in progress
How do you find out when and why a strange condition in a method was added?
git blame shows the last commit that changed a line, but formatting or moving can mislead it. So I follow a function's history with blame -w -C, git log -S/-G (pickaxe) and git log -L.
📖 Answer
A plain git blame often points at a commit like "someone sorted the imports". Useful variants:
# ignore whitespace, and track code moved from another file
git blame -w -C -C src/main/java/shop/PriceService.java
# when this line appeared/disappeared
git log -S "if (amount.signum() < 0)" --oneline
# search inside diffs by regex
git log -G "retry(Count|Limit)" -p
# the whole evolution of a single method
git log -L :calculateDiscount:src/main/java/shop/PriceService.java
-S finds commits where the number of occurrences of the text changed (added or removed), while -G finds any commit where the regex appears in the diff.
Once you find the commit, the most valuable information is often not in the code but in the message and in the linked PR/ticket. That is why messages like "fix" cost you later.
If there are big formatting commits, put them in .git-blame-ignore-revs and run git config blame.ignoreRevsFile .git-blame-ignore-revs. GitHub recognizes this file too.
Sign up to mark studied questions and save your progress.