brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git log -G finds commits whose changes match a regular expression

Git
~/tips/tip-git-log-g-regex-pickaxe

git log -G'timeout\s*=' --oneline -p -- config/

Finds commits whose added or removed lines match a regular expression.

I like git log -S for finding where a string appeared, but it only counts occurrences. When the line was edited and the string stayed, -S sees nothing. -G looks at the changed lines themselves.

git log -G'timeout\s*=' --oneline -p -- config/

The manual says -G looks for differences whose patch text contains added or removed lines that match the regular expression. So a commit that changes timeout = 5 into timeout = 30 is found, even though the number of timeout = occurrences did not change. -S would skip it, because it only finds commits that change how many times the string occurs.

Add -p to see the diff and -- path to limit the search to part of the tree, which also makes it faster. Searching a large history for a loose pattern can take a while, because git has to look at every patch.

Caveats: the regular expression is applied to the lines in the diff, so a pattern that spans two lines will not match. Binary files are ignored unless you pass --text. And quote the pattern, or the shell will eat the backslashes and asterisks.

Use -S for “when did this string appear or vanish” and -G for “when did this kind of line change”.

Further reading: git log, -G and -S (Git documentation)

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link