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

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

git log -p | grep <pattern>

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


当前回答

好吧,就在今天,我见过两次有人想要一个更接近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平分。使用平分线,您可以指定已知的好版本、已知的坏版本,以及一个简单的脚本,该脚本可以检查版本是好还是坏(在本例中,一个grep可以查看您正在查找的代码是否存在)。运行此命令将发现代码何时被删除。

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

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 ;) )

在git历史记录中搜索的命令

git log -S"alter" --author="authorname" --since=2021.1.1 --until=2023.1.1 -- .

每当我找到你的位置时,我都会使用以下命令行:

git log -S "<words/phrases i am trying to find>" --all --oneline  --graph

说明:

gitlog-我需要在这里写更多;它按时间顺序显示日志。-S“<单词/短语我试图查找>”-它显示了所有Git提交,其中任何文件(添加/修改/删除)都包含我试图查找的单词/短语,但没有“<>”符号。--all-在所有分支中强制执行和搜索。--oneline-它将Git日志压缩为一行。--graph-它创建按时间顺序提交的图形。

Jeet的答案在PowerShell中有效。

git grep -n <regex> $(git rev-list --all)

下面显示了任何提交中包含密码的所有文件。

# Store intermediate result
$result = git grep -n "password" $(git rev-list --all)

# Display unique file names
$result | select -unique { $_ -replace "(^.*?:)|(:.*)", "" }