我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
我曾经删除过一个文件或文件中的一些代码。我可以在内容(而不是提交消息)中进行grep吗?
一个非常糟糕的解决方案是grep日志:
git log -p | grep <pattern>
然而,这不会立即返回提交哈希。我和吉特·格里普玩得不亦乐乎。
当前回答
对于其他试图在Sourcetree中执行此操作的用户,UI中没有直接命令(从1.6.21.0版起)。但是,您可以通过打开终端窗口(主工具栏中可用的按钮)并将其复制/粘贴到其中来使用接受答案中指定的命令。
注意:Sourcetree的“搜索”视图可以部分为您进行文本搜索。按Ctrl+3转到“搜索”视图(或单击底部可用的“搜索”选项卡)。从最右边开始,将“搜索类型”设置为“文件更改”,然后键入要搜索的字符串。与上述命令相比,此方法具有以下限制:
Sourcetree只显示其中一个已更改文件中包含搜索词的提交。查找包含搜索文本的确切文件也是一项手动任务。不支持RegEx。
其他回答
好吧,就在今天,我见过两次有人想要一个更接近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);
我接受了Jeet的答案,并将其改编为Windows(多亏了这个答案):
FOR /F %x IN ('"git rev-list --all"') DO @git grep <regex> %x > out.txt
注意,对于我来说,出于某种原因,删除这个正则表达式的实际提交并没有出现在命令的输出中,而是出现在它之前的一次提交。
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很有用,因为它只接受文件名,而不接受相对/系统绝对路径。
在这里查看我的完整答案以了解更多解释。
我在这里有点惊讶,也许我错过了我正在寻找的答案,但我来到这里是为了寻找所有树枝的顶端。并不是针对存储库中的每一个修订,所以对我来说,使用git-rev列表——所有信息都太多了。
换句话说,对我来说,最有用的变化是
git grep -i searchString $(git branch -r)
or
git branch -r | xargs git grep -i searchString
or
git branch -r | xargs -n1 -i{} git grep -i searchString {}
当然,您可以在这里尝试正则表达式方法。这里的方法很酷的是,它直接针对远程分支。我不必在这些分支机构中的任何一家进行检查。
灵感来自答案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