how to find the merge where your code disappeared

Because I keep forgetting that I originally put this on the twitters…​

git log --patch -m -G regexForMissingCode

(Neither of those short options have long names…​)

About -m:

This flag makes the merge commits show the full diff like regular commits; for each merge parent, a separate log entry and diff is generated. An exception is that only diff against the first parent is shown when --first-parent option is given; in that case, the output represents the changes the merge brought into the then-current branch.

— git help log

About -G <regex>:

Look for differences whose patch text contains added/removed lines that match <regex>. To illustrate the difference between -S<regex> --pickaxe-regex and -G<regex>, consider a commit with the following diff in the same file:

+    return !regexec(regexp, two->ptr, 1, &regmatch, 0);
-    hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);

While git log -G"regexec\(regexp" will show this commit, git log -S"regexec\(regexp" --pickaxe-regex will not (because the number of occurrences of that string did not change). See the pickaxe entry in gitdiffcore(7) for more information.

— git help log

If you find that you’re digging through third-party code or compiled stuff, here’s a handy way of excluding certain directories!

Share Comment on Twitter
comments powered by Disqus