我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
当前回答
A.完整、唯一、排序的路径:
# Get all unique filepaths of files matching 'password'
# Source: https://stackoverflow.com/a/69714869/10830091
git rev-list --all | (
while read revision; do
git grep -F --files-with-matches 'password' $revision | cat | sed "s/[^:]*://"
done
) | sort | uniq
B.唯一、排序的文件名(不是路径):
# Get all unique filenames matching 'password'
# Source: https://stackoverflow.com/a/69714869/10830091
git rev-list --all | (
while read revision; do
git grep -F --files-with-matches 'password' $revision | cat | sed "s/[^:]*://"
done
) | xargs basename | sort | uniq
第二个命令对BFG很有用,因为它只接受文件名,而不接受相对/系统绝对路径。
在这里查看我的完整答案以了解更多解释。
其他回答
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 "(^.*?:)|(:.*)", "" }
好吧,就在今天,我见过两次有人想要一个更接近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);
A.完整、唯一、排序的路径:
# Get all unique filepaths of files matching 'password'
# Source: https://stackoverflow.com/a/69714869/10830091
git rev-list --all | (
while read revision; do
git grep -F --files-with-matches 'password' $revision | cat | sed "s/[^:]*://"
done
) | sort | uniq
B.唯一、排序的文件名(不是路径):
# Get all unique filenames matching 'password'
# Source: https://stackoverflow.com/a/69714869/10830091
git rev-list --all | (
while read revision; do
git grep -F --files-with-matches 'password' $revision | cat | sed "s/[^:]*://"
done
) | xargs basename | sort | uniq
第二个命令对BFG很有用,因为它只接受文件名,而不接受相对/系统绝对路径。
在这里查看我的完整答案以了解更多解释。
场景:您使用IDE对代码进行了大清理。问题:IDE清理的比它应该清理的多,现在代码无法编译(缺少资源等)
解决方案:
git grep --cached "text_to_find"
它将找到更改“text_to_find”的文件。
现在可以撤消此更改并编译代码。
如果您想浏览代码更改(查看整个历史中给定单词的实际更改),请选择补丁模式-我发现了一个非常有用的组合:
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 ;) )