I have deleted a file or some code in a file sometime in the past. Can I search through the content (not just the commit messages)?
A very poor solution is to grep the log:
git log -p | grep <pattern>
However, this doesn’t return the commit hash straight away. I played around with git grep to no avail.
You should use the pickaxe (
-S) option ofgit log.To search for
Foo:See Git history – find lost line by keyword for more.
-S(namedpickaxe) comes originally from agit diffoption (Git v0.99, May 2005).Then
-S(pickaxe) was ported togit login May 2006 with Git 1.4.0-rc1.As Jakub Narębski commented:
this looks for differences that introduce or remove an instance of
<string>.It usually means "revisions where you added or removed line with ‘Foo’".
the
--pickaxe-regexoption allows you to use extended POSIX regex instead of searching for a string.Example (from
git log):git log -S"frotz\(nitfol" --pickaxe-regexAs Rob commented, this search is case-sensitive – he opened a follow-up question on how to search case-insensitive.
Hi Angel notes in the comments: