我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
当前回答
场景:您使用IDE对代码进行了大清理。问题:IDE清理的比它应该清理的多,现在代码无法编译(缺少资源等)
解决方案:
git grep --cached "text_to_find"
它将找到更改“text_to_find”的文件。
现在可以撤消此更改并编译代码。
其他回答
灵感来自答案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
场景:您使用IDE对代码进行了大清理。问题:IDE清理的比它应该清理的多,现在代码无法编译(缺少资源等)
解决方案:
git grep --cached "text_to_find"
它将找到更改“text_to_find”的文件。
现在可以撤消此更改并编译代码。
每当我找到你的位置时,我都会使用以下命令行:
git log -S "<words/phrases i am trying to find>" --all --oneline --graph
说明:
gitlog-我需要在这里写更多;它按时间顺序显示日志。-S“<单词/短语我试图查找>”-它显示了所有Git提交,其中任何文件(添加/修改/删除)都包含我试图查找的单词/短语,但没有“<>”符号。--all-在所有分支中强制执行和搜索。--oneline-它将Git日志压缩为一行。--graph-它创建按时间顺序提交的图形。
好吧,就在今天,我见过两次有人想要一个更接近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);
对于其他试图在Sourcetree中执行此操作的用户,UI中没有直接命令(从1.6.21.0版起)。但是,您可以通过打开终端窗口(主工具栏中可用的按钮)并将其复制/粘贴到其中来使用接受答案中指定的命令。
注意:Sourcetree的“搜索”视图可以部分为您进行文本搜索。按Ctrl+3转到“搜索”视图(或单击底部可用的“搜索”选项卡)。从最右边开始,将“搜索类型”设置为“文件更改”,然后键入要搜索的字符串。与上述命令相比,此方法具有以下限制:
Sourcetree只显示其中一个已更改文件中包含搜索词的提交。查找包含搜索文本的确切文件也是一项手动任务。不支持RegEx。