使'git log'忽略某些路径的更改

Ano*_*ose 108 git

git log除了我指定的文件外,我怎么才能只显示改变文件的提交?

有了git log,我可以将我看到的提交过滤给那些触及给定路径的人.我想要的是反转该过滤器,以便只列出除指定的触摸路径以外的触摸路径.

我可以得到我想要的东西

git log --format="%n/%n%H" --name-only | ~/filter-log.pl | git log --stdin --no-walk
Run Code Online (Sandbox Code Playgroud)

在哪里filter-log.pl:

#!/usr/bin/perl
use strict;
use warnings;

$/ = "\n/\n";
<>;

while (<>) {
    my ($commit, @files) = split /\n/, $_;

    if (grep { $_ && $_ !~ m[^(/$|.etckeeper$|lvm/(archive|backup)/)] } @files) {
        print "$commit\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

除了我想要比这更优雅的东西.

请注意,我不是在问如何让git忽略这些文件.跟踪和提交这些文件.就是这样,大多数时候,我对看到它们并不感兴趣.

相关问题:如何反转`git log --grep = <pattern>`或如何显示与模式不匹配的git日志除了提交消息而不是路径之外,它是同一个问题.

关于这个主题的论坛讨论从2008年开始:Re:从git-diff中排除文件这看起来很有希望,但线程似乎已经枯竭了.

Von*_*onC 191

它现在已经实现了(git 1.9/2.0,2014年第一季度),引用pathspec magic :(exclude)及其缩写形式:!提交ef79b1f提交1649612,由 NguyễnTháiNgọcDuy(pclouds),文档可以在这里找到.

您现在可以记录除子文件夹内容之外的所有内容:

git log -- . ":(exclude)sub"
git log -- . ":!sub"
Run Code Online (Sandbox Code Playgroud)

或者,您可以排除该子文件夹中的特定元素

您可以使该排除案例不敏感!

git log -- . ":(exclude,icase)SUB"
Run Code Online (Sandbox Code Playgroud)

正如Kenny Evitt所说

如果您在Bash shell中运行Git,请使用':!sub'":\!sub"改为避免bash: ... event not found错误


注:Git的2.13(Q2 2017)将同义词添加^!

请参阅Linus Torvalds()提交859b7f1,提交42ebeb9(2017年2月8日).(由Junio C Hamano合并- -提交015fba3,2017年2月27日)torvalds
gitster

pathspec法宝:添加" ^"作为别名" !"

选择' !'作为负路径规范最终不仅不匹配我们为修订做的事情,它也是shell扩展的可怕特征,因为它需要引用.

因此,添加' ^'作为排除pathspec条目的替代别名.

  • @JustinThomas我相信(尚未测试)您可以多次重复该路径排除模式`":( exclude)pathPattern1"":( exclude)pathPattern2"`,因此忽略多个文件夹/文件. (10认同)
  • 你能做多个文件吗? (7认同)
  • 如果你在Bash shell中运行Git,请使用`':!sub'来代替[避免`bash:... event not found` errors](http://unix.stackexchange.com/questions/3051/如何到回波-A-爆炸).`":\!sub"`不起作用. (7认同)
  • @KennyEvitt 感谢您的编辑和评论。我已将后者包含在答案中以获得更多可见性。 (2认同)
  • 对于那些想知道关于这个功能的官方文档在哪里的人,请参阅`git help glossary`(我在`git help -g`中找到了[我在`git help`中找到的建议]). (2认同)

l0b*_*0b0 5

tl; dr: shopt -s extglob && git log !(unwanted/glob|another/unwanted/glob)

如果您使用的是Bash,则应该能够使用扩展的globlobing功能来仅获取所需的文件:

$ cd -- "$(mktemp --directory)" 
$ git init
Initialized empty Git repository in /tmp/tmp.cJm8k38G9y/.git/
$ mkdir aye bee
$ echo foo > aye/foo
$ git add aye/foo
$ git commit -m "First commit"
[master (root-commit) 46a028c] First commit
 0 files changed
 create mode 100644 aye/foo
$ echo foo > bee/foo
$ git add bee/foo
$ git commit -m "Second commit"
[master 30b3af2] Second commit
 1 file changed, 1 insertion(+)
 create mode 100644 bee/foo
$ shopt -s extglob
$ git log !(bee)
commit ec660acdb38ee288a9e771a2685fe3389bed01dd
Author: My Name <jdoe@example.org>
Date:   Wed Jun 5 10:58:45 2013 +0200

    First commit
Run Code Online (Sandbox Code Playgroud)

您可以将其与globstar递归操作结合使用。

  • 这不会显示影响文件的提交。非常接近,不错,一切都一样。 (7认同)