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

时间:2011-04-16 06:52:53

标签: git

如何让git log仅显示更改了我指定的文件的提交?

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

我可以得到我想要的东西

git log --format="%n/%n%H" --name-only | ~/filter-log.pl | git log --stdin --no-walk

其中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";
    }
}

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

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

相关问题:How to invert `git log --grep=<pattern>` or How to show git logs that don't match a pattern除了提交消息而不是路径之外,它是同一个问题。

2008年关于这个主题的论坛讨论:Re: Excluding files from git-diff这看起来很有希望,但线索似乎已经枯竭。

3 个答案:

答案 0 :(得分:176)

现在已经实施(git 1.9 / 2.0,2014年第一季度),在commit ef79b1fcommit 1649612中引入了 pathspec magic :(exclude)及其简短形式:! {3}},by Nguyễn Thái Ngọc Duy (pclouds),可以找到文档here

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

git log -- . ":(exclude)sub"
git log -- . ":!sub"

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

  • 特定文件:

    git log -- . ":(exclude)sub/sub/file"
    git log -- . ":!sub/sub/file"
    
  • sub中的任何给定文件:

    git log -- . ":(exclude)sub/*file"
    git log -- . ":!sub/*file"
    git log -- . ":(exclude,glob)sub/*/file"
    

您可以对排除案例不敏感!

git log -- . ":(exclude,icase)SUB"

作为Kenny Evitt noted

  

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


注意:Git 2.13(2017年第2季度)会将同义词^添加到!

commit 859b7f1commit 42ebeb9Linus Torvalds (torvalds)(2017年2月8日) Junio C Hamano -- gitster --于2017年2月27日commit 015fba3合并)

  

pathspec magic:添加“^”作为“!”的别名

     

对于负路径规则选择“!”最终不仅不匹配   我们为修改做了什么,它对于shell来说也是一个可怕的角色   扩展,因为它需要引用。

因此,添加“^”作为排除pathspec条目的替代别名。

答案 1 :(得分:4)

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

如果您使用 Bash ,则应该可以使用 extended globbing 功能来获取所需的文件:

$ 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

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

答案 2 :(得分:0)

您可以暂时忽略文件中的更改:

git update-index --skip-worktree path/to/file

展望未来,git statusgit commit -a等将忽略对这些文件的所有更改。当您准备提交这些文件时,只需将其反转:

git update-index --no-skip-worktree path/to/file

并正常提交。

相关问题