「git log --exclude-author」的含义是什么?

71

我在工作中使用一个Git仓库,其中大部分提交都是由机器人用户自动提交的。有时候我希望查看该仓库的Git日志,但不想看到自动提交的记录。如果存在这样的选项,它可以被描述为反转的“git log --author”或“git log --exclude-author=botuser”。

目前我的解决方案如下,快捷键已设置为Bash别名。

git log --format="%H %aE" | grep -v -F botuser@domain | while read hash email; do git log -1 $hash; echo; done | less

我的问题是,是否有一种不太hack的解决方案来实现我想要的目标?


1
如果你仍然经常访问这个网站,我建议你更改这个问题的被接受答案。Hammer只是告诉你它正在考虑纳入git中,而quodlibetor实际上提供了一个解决方案。 - thecoshman
3
为了完整起见,现在可以使用git log --author=bot --invert-grep命令。该命令的作用是筛选出除"bot"以外的作者所做的提交记录,并将结果输出。 - larhat
2
@larhat 警告:这个命令(git log --author=bot --invert-grep)将在 Git 2.35+ (2022年第一季度) 开始失效。 - VonC
6个回答

64

来自https://coderwall.com/p/tzdzwa:

git log --perl-regexp --author='^((?!excluded-author-regex).*)$'

这对我有用。

如果你不想每次都指定 --perl-regexp,可以这样做:

git config --global grep.patternType perl

2
这需要git 1.8.0或更高版本。 - Flimm
工作正常。您可以使用 --perl-regexp 代替更改 grep 模式类型的全局配置。 - jmkk
不幸的是,在 Mac 上我收到了 fatal: cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE 的错误。我认为我需要按照这个来做 https://dev59.com/A2Eh5IYBdhLWcg3wwly8#28587310。 - Hakan Baba
如果你收到cannot use Perl-compatible regexes...的错误提示,而this没能帮助你解决问题,尝试运行brew upgrade git。这个方法对我很有用。 - Guy
1
这似乎对作者电子邮件无效,仅适用于作者姓名。 - jyn
嗯,实际上它似乎在连接作者姓名和电子邮件。git log --perl-regexp --author='^((?!bors.*bors@rust-lang.org).*)$'可以做正确的事情,但git log --perl-regexp --author='^((?!bors@rust-lang.org).*)$'不能。 - jyn

53

其他答案提到了部分内容,但是我在我的代码库中使用了一个我在这里看到的策略,并且取得了成功。

 git log --invert-grep --author=<pattern>

您可以添加所需数量的作者模式。在我的情况下,最佳模式是完整的电子邮件,因为它被认为是唯一的。


1
这正是我想要的。谢谢! - Harrison McCullough
13
注意:这个方法不再适用于Git 2.35+(2022年第一季度)以后的版本。 - VonC

25

警告,mateoranswer将不再与Git 2.35(2022年第一季度)兼容。

 # WRONG:
 git log --invert-grep --author=<pattern>

quodlibetoranswer仍然有效:

git log --perl-regexp --author='^((?!excluded-author-regex).*)$'

但是:不再使用git log --invert-grep --author=<pattern>来排除某个作者。

"git log --invert-grep --author=<name>"(手册)曾用于排除给定作者所写的提交,但现在"--invert-grep"只影响由"--grep=<pattern>"选项匹配到的内容。

参见提交 794c000 (2021年12月17日) by René Scharfe (rscharfe)
(由Junio C Hamano -- gitster --提交 2043ce8中合并,2022年1月5日)

log: 只让--invert-grep反转--grep

报告者: Dotan Cohen
签名: René Scharfe

选项--invert-grep的文档说明是过滤掉符合--grep过滤器的提交消息。
然而,它也会影响标题匹配(--author--committer),这并不是预期的行为。
将该选项的处理移至grep.c, 因为只有那里的代码才能区分标题中的匹配和正文中的匹配。
如果给出--invert-grep,则启用扩展表达式(不是正则表达式类型,我们只需要 'git grep'(man)s --not 即可工作),否定正文模式并检查它们是否匹配,通过利用grep_source_1()collect_hits机制来实现。
grep_opt结构体中收集匹配结果有点棘手,但是通过"last_shown"我们已经为向该结构体写入状态信息设立了先例。

为什么?在2017年6月的初步讨论之后,这个问题再次于2021年12月进行了讨论

What did you do before the bug happened?

$ git log -8 --author=Shachar --grep=Revert --invert-grep

What did you expect to happen?

I expected to see the last 8 commits from Shachar that did not have the string "Revert" in the commit message.

What happened instead?

The list of commits included commits by authors other than Shachar.

What's different between what you expected and what actually happened?

The "--author" filter seems to be ignored when the "--invert-grep" option is used.
I also tried to change the order of the options, but the results remained the same.


一方面,将反转标志传递到支持“--author”的grep显然是一种hack。但另一方面,“--invert-grep”应用于每个其他模式,为什么对使用辅助标志设置的模式不适用呢? - mateor
1
@mateor 我同意。我发现你的解决方案更直观。 - VonC
:'( 那真遗憾 - CervEd

17

1

在 Git 2.35+ 中兼容的替代解决方案可以在这里找到。

git log --format='%H %an' |  # get a list of all commit hashes followed by the author name
  grep -v Adam |             # match the name but return the lines that *don't* contain the name
  cut -d ' ' -f1 |           # from this extract just the first part of the line which is commit ref
  xargs -n1 git log          # call git log 

0
另一种方法是使用格式化的--pretty标志。
  • 示例: git log --pretty=format:"ID Commit %h, %ar%nThe title was >>%s<<%n"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接