[英]How to do `git grep` only in files I have committed?
Is there a way to make git grep
only match files that I have committed? 有没有办法使
git grep
只匹配我提交的文件?
The purpose is to grep in the contents of the file, not the log message. 目的是grep输入文件的内容,而不是日志消息。
author="your name"
git log --pretty="%H" --author="$author" | while read commit_hash; do git show --oneline --name-only $commit_hash | tail -n+2; done | sort | uniq |xargs grep -i "String to grep"
OR shorter version: 或更短的版本:
author="your name"
git log --no-merges --author="$author" --name-only --pretty=format:"" | sort -u |xargs grep -i <string>
Orignal answer used to get the files from a particular user 用于从特定用户获取文件的原始答案
If you want to grep in the diff of a commit, you should probably use git log -G
or git log -S
: 如果要在提交的差异中进行grep,则可能应使用
git log -G
或git log -S
:
git log -p --author="your name" -G pattern
Both -G
and -S
will look for the pattern in the diff introduced by a commit, -G
和-S
都会在提交引入的差异中查找模式,
the difference is ( git help log
) : 区别是(
git help log
):
To illustrate the difference between
-S<regex> --pickaxe-regex
and-G<regex>
, consider a commit with the following diff in the same file:为了说明
-S<regex> --pickaxe-regex
和-G<regex>
之间的区别,请考虑在同一文件中包含以下差异的提交:+ return !regexec(regexp, two->ptr, 1, ®match, 0); ... - hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
While
git log -G"regexec\\(regexp"
will show this commit,git log -S"regexec\\(regexp" --pickaxe-regex
will not (because the number of occurrences of that string did not change).尽管
git log -G"regexec\\(regexp"
将显示此提交,而git log -S"regexec\\(regexp" --pickaxe-regex
则不会)(因为该字符串的出现次数未更改)。
When using -p
in conjunction with -G
or -S
, only the files matching the pattern will be displayed. 当将
-p
与-G
或-S
结合使用时,将仅显示与该模式匹配的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.