如何搜索有人用 git 更改的字符串?

How can I search for a string that someone changed with git?

我想使用 git grep 搜索特定的字符串,但我想使用 git blame 过滤结果以了解我要查找的字符串已被特定的更改人。

我只是不知道如何将它们组合起来才能得到我想要的结果。

感谢您的帮助。

您可以编写一些 shell 脚本来完成此操作:

git rev-list --author=Doe HEAD |
while read rev; do
    if git show -p $rev | grep "PATTERN" >/dev/null; then
        echo $rev
    fi
done

这将输出作者为 "Doe" 并在提交内容中具有 "PATTERN" 的 HEAD 可访问的 SHA。

这应该可以满足您的要求。

author="Some User"
searchstring=string
searchfiles=(file1 file2 file3) # Leave empty for all files.

while IFS= read -rd '' file; read -rd '' nr; read -r line; do
    if git annotate -p -L "$nr,$nr" -- "$file" | grep -q "$author"; then
        printf '%s:%s:%s\n' "$file" "$nr" "$line"
    fi
done < <(git grep -nz "$searchstring" -- "${searchfiles[@]}")

这是否 better/faster 比 Jonathan.Brink 的 更有效取决于该行的匹配量、历史的大小、作者提交的内容在历史中,更改是在最近还是最近提交的作者等

使用 git grep -z 确保任意文件名的安全,使用 read -d '' 读取那些 NUL 分隔的字段。

使用git annotate -L限制需要注释的行。

输出是原始 git grep -n 输出,但仅用于作者匹配的行。