[英]How to do `git grep` only in files I have committed?
有沒有辦法使git grep
只匹配我提交的文件?
目的是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"
或更短的版本:
author="your name"
git log --no-merges --author="$author" --name-only --pretty=format:"" | sort -u |xargs grep -i <string>
用於從特定用戶獲取文件的原始答案
如果要在提交的差異中進行grep,則可能應使用git log -G
或git log -S
:
git log -p --author="your name" -G pattern
-G
和-S
都會在提交引入的差異中查找模式,
區別是( git help log
):
為了說明
-S<regex> --pickaxe-regex
和-G<regex>
之間的區別,請考慮在同一文件中包含以下差異的提交:+ return !regexec(regexp, two->ptr, 1, ®match, 0); ... - hit = !regexec(regexp, mf2.ptr, 1, ®match, 0);
盡管
git log -G"regexec\\(regexp"
將顯示此提交,而git log -S"regexec\\(regexp" --pickaxe-regex
則不會)(因為該字符串的出現次數未更改)。
當將-p
與-G
或-S
結合使用時,將僅顯示與該模式匹配的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.