I have deleted a file or some code in a file sometime in the past. Can I grep in the content (not in 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.
So are you trying to grep through older versions of the code looking to see where something last exists?
If I were doing this, I would probably use git bisect. Using bisect, you can specify a known good version, a known bad version, and a simple script that does a check to see if the version is good or bad (in this case a grep to see if the code you are looking for is present). Running this will find when the code was removed.
If you want to browse code changes (see what actually has been changed with the given word in the whole history) go for
patch
mode - I found a very useful combination of doing:@Jeet's answer works in PowerShell.
The following displays all files, in any commit, that contain a
password
.I took @Jeet's answer and adpated it to Windows (thanks to this answer):
Note that for me, for some reason, the actual commit that deleted this regex did not appear in the output of the command, but rather one commit prior to it.
You should use the pickaxe (
-S
) option ofgit log
To search for
Foo
:See Git history - find lost line by keyword for more.
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-regex
option allows you to use extended POSIX regex instead of searching for a string.As Rob commented, this search is case-sensitive - he opened a follow-up question on how to search case-insensitive.
In my case I needed to search a Short Commit and the listed solutions were unfortunately not working.
I managed to do it with: (replace the REGEX token)