簡體   English   中英

如何只在我提交的文件中執行`git grep`?

[英]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 -Ggit 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, &regmatch, 0); ... - hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0); 

盡管git log -G"regexec\\(regexp"將顯示此提交,而git log -S"regexec\\(regexp" --pickaxe-regex則不會)(因為該字符串的出現次數未更改)。

當將-p-G-S結合使用時,將僅顯示與該模式匹配的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM