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

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

git log -p | grep <pattern>

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


当前回答

为了简单起见,我建议使用GUI:gitk——Git存储库浏览器。它很灵活

要搜索代码:要搜索文件,请执行以下操作:当然,它还支持正则表达式:

您可以使用向上/向下箭头浏览结果。

其他回答

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

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

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

好吧,就在今天,我见过两次有人想要一个更接近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可以查看您正在查找的代码是否存在)。运行此命令将发现代码何时被删除。

灵感来自答案https://stackoverflow.com/a/2929502/6041515,我发现gitgrep似乎在每次提交时都会搜索完整的代码库,而不仅仅是差异,结果往往重复且冗长。下面的脚本将只搜索每个git提交的差异:

for commit in $(git rev-list --all); do 
    # search only lines starting with + or -
    if  git show "$commit" | grep "^[+|-].*search-string"; then 
        git show --no-patch --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short $commit
    fi  
done

示例输出,底部的gitcommit是第一次引入我正在搜索的更改的gitcommit:

csshx$ for commit in $(git rev-list --all); do 
>     if  git show "$commit" | grep "^[+|-].*As csshX is a command line tool"; then 
>         git show --no-patch --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short $commit
>     fi  
> done

+As csshX is a command line tool, no special installation is needed. It may
987eb89 2009-03-04 Gavin Brock Added code from initial release

对于其他试图在Sourcetree中执行此操作的用户,UI中没有直接命令(从1.6.21.0版起)。但是,您可以通过打开终端窗口(主工具栏中可用的按钮)并将其复制/粘贴到其中来使用接受答案中指定的命令。

注意:Sourcetree的“搜索”视图可以部分为您进行文本搜索。按Ctrl+3转到“搜索”视图(或单击底部可用的“搜索”选项卡)。从最右边开始,将“搜索类型”设置为“文件更改”,然后键入要搜索的字符串。与上述命令相比,此方法具有以下限制:

Sourcetree只显示其中一个已更改文件中包含搜索词的提交。查找包含搜索文本的确切文件也是一项手动任务。不支持RegEx。