我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?

一个非常糟糕的解决方案是grep日志:

git log -p | grep <pattern>

然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。


当前回答

我接受了Jeet的答案,并将其改编为Windows(多亏了这个答案):

FOR /F %x IN ('"git rev-list --all"') DO @git grep <regex> %x > out.txt

注意,对于我来说,出于某种原因,删除这个正则表达式的实际提交并没有出现在命令的输出中,而是出现在它之前的一次提交。

其他回答

如果您想浏览代码更改(查看整个历史中给定单词的实际更改),请选择补丁模式-我发现了一个非常有用的组合:

git log -p
# Hit '/' for search mode.
# Type in the word you are searching.
# If the first search is not relevant, hit 'n' for next (like in Vim ;) )

我接受了Jeet的答案,并将其改编为Windows(多亏了这个答案):

FOR /F %x IN ('"git rev-list --all"') DO @git grep <regex> %x > out.txt

注意,对于我来说,出于某种原因,删除这个正则表达式的实际提交并没有出现在命令的输出中,而是出现在它之前的一次提交。

好吧,就在今天,我见过两次有人想要一个更接近hggrep的等价物,它类似于git-log-pS,但它的输出仅限于(注释的)更改的行。

我想,如果你快速浏览一下,这会比寻呼机中的/模式/更方便。

所以这里有一个diff hunk扫描器,它接收git-log--prey=%h-p输出并输出带注释的更改行。把它放在diffmarkup.l中,比如make~/bin/diffmarkup,然后像这样使用

git log --pretty=%h -pS pattern | diffmarkup | grep pattern
%option main 8bit nodefault
        // vim: tw=0
%top{
        #define _GNU_SOURCE 1
}
%x commitheader
%x diffheader
%x hunk
%%
        char *afile=0, *bfile=0, *commit=0;
        int aline,aremain,bline,bremain;
        int iline=1;

<hunk>\n        ++iline; if ((aremain+bremain)==0) BEGIN diffheader;
<*>\n   ++iline;

<INITIAL,commitheader,diffheader>^diff.*        BEGIN diffheader;
<INITIAL>.*     BEGIN commitheader; if(commit)free(commit); commit=strdup(yytext);
<commitheader>.*

<diffheader>^(deleted|new|index)" ".*   {}
<diffheader>^"---".*            if (afile)free(afile); afile=strdup(strchrnul(yytext,'/'));
<diffheader>^"+++".*            if (bfile)free(bfile); bfile=strdup(strchrnul(yytext,'/'));
<diffheader,hunk>^"@@ ".*       {
        BEGIN hunk; char *next=yytext+3;
        #define checkread(format,number) { int span; if ( !sscanf(next,format"%n",&number,&span) ) goto lostinhunkheader; next+=span; }
        checkread(" -%d",aline); if ( *next == ',' ) checkread(",%d",aremain) else aremain=1;
        checkread(" +%d",bline); if ( *next == ',' ) checkread(",%d",bremain) else bremain=1;
        break;
        lostinhunkheader: fprintf(stderr,"Lost at line %d, can't parse hunk header '%s'.\n",iline,yytext), exit(1);
        }
<diffheader>. yyless(0); BEGIN INITIAL;

<hunk>^"+".*    printf("%s:%s:%d:%c:%s\n",commit,bfile+1,bline++,*yytext,yytext+1); --bremain;
<hunk>^"-".*    printf("%s:%s:%d:%c:%s\n",commit,afile+1,aline++,*yytext,yytext+1); --aremain;
<hunk>^" ".*    ++aline, ++bline; --aremain; --bremain;
<hunk>. fprintf(stderr,"Lost at line %d, Can't parse hunk.\n",iline), exit(1);

为现有答案添加更多内容。如果您知道您可能制作的文件,请执行以下操作:

git log --follow -p -S 'search-string' <file-path>

--如下:列出文件的历史记录

我最喜欢的方法是使用gitlog的-G选项(在1.7.4版本中添加)。

-G<regex>
       Look for differences whose added or removed line matches the given <regex>.

-G和-S选项确定提交是否匹配的方式存在细微差异:

-S选项本质上统计提交前后搜索在文件中匹配的次数。如果前后计数不同,则提交将显示在日志中。例如,这不会显示移动了与搜索匹配的行的提交。使用-G选项,如果您的搜索与添加、删除或更改的任何行相匹配,则提交将显示在日志中。

以此次提交为例:

diff --git a/test b/test
index dddc242..60a8ba6 100644
--- a/test
+++ b/test
@@ -1 +1 @@
-hello hello
+hello goodbye hello

因为在提交之前和之后“hello”在文件中出现的次数相同,所以使用-Shello将不匹配。然而,由于对匹配hello的行进行了更改,提交将使用-Gello显示。