使“git log”忽略某些路径的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5685007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making 'git log' ignore changes for certain paths
提问by Anonymoose
How can I make git log
only show commits that changed files other than the ones I specify?
如何git log
仅显示更改了我指定的文件以外的文件的提交?
With git log
, I can filter the commits I see to those that touch a given set of paths. What I want is to invert that filter so that only commits that touch paths other than the specified ones will be listed.
使用git log
,我可以将我看到的提交过滤到那些触及给定路径集的提交。我想要的是反转该过滤器,以便只列出指定路径以外的触摸路径。
I can get what I want with
我可以得到我想要的
git log --format="%n/%n%H" --name-only | ~/filter-log.pl | git log --stdin --no-walk
where filter-log.pl
is:
在哪里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";
}
}
except I want something somewhat more elegant than that.
除了我想要一些比这更优雅的东西。
Note that I am notasking how to make git ignore the files. These files shouldbe tracked and committed. It's just that, most of the time, I'm not interested in seeing them.
请注意,我不是在问如何让 git 忽略文件。应该跟踪和提交这些文件。只是,大多数时候,我对看到它们并不感兴趣。
Related question: How to invert `git log --grep=<pattern>` or How to show git logs that don't match a patternIt's the same question except for commit messages rather than paths.
相关问题:如何反转 `git log --grep=<pattern>` 或如何显示与模式不匹配的 git 日志除了提交消息而不是路径之外,这是同一个问题。
Forum discussion on this subject from 2008: Re: Excluding files from git-diffThis looked promising but the thread seems to have dried up.
2008 年关于这个主题的论坛讨论:Re: Exclusion files from git-diff这看起来很有希望,但线程似乎已经枯竭。
回答by VonC
It is implemented now (git 1.9/2.0, Q1 2014) with the introduction pathspec magic :(exclude)
and its short form :!
in commit ef79b1fand commit 1649612, by
Nguy?n Thái Ng?c Duy (pclouds
), documentation can be found here.
它现在已实现(git 1.9/ 2.0,2014 年:(exclude)
:!
第一季度),引入pathspec magic及其简短形式在commit ef79b1f和commit 1649612 中,由
Nguy?n Thái Ng?c Duy ( pclouds
)编写,文档可以在这里找到。
You now can log everything except a sub-folder content:
您现在可以记录除子文件夹内容之外的所有内容:
git log -- . ":(exclude)sub"
git log -- . ":!sub"
Or you can exclude specific elements within that sub-folder
或者您可以排除该子文件夹中的特定元素
a specific file:
git log -- . ":(exclude)sub/sub/file" git log -- . ":!sub/sub/file"
any given file within
sub
:git log -- . ":(exclude)sub/*file" git log -- . ":!sub/*file" git log -- . ":(exclude,glob)sub/*/file"
一个特定的文件:
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"
You can make that exclusion case insensitive!
您可以使排除不区分大小写!
git log -- . ":(exclude,icase)SUB"
If you're running Git in a Bash shell, use
':!sub'
or":\!sub"
instead to avoidbash: ... event not found
errors
如果您在 Bash shell 中运行 Git,请使用
':!sub'
或":\!sub"
来避免bash: ... event not found
错误
Note: Git 2.13 (Q2 2017) will add a synonym ^
to !
注:Git的2.13(Q2 2017)将同义词添加^
到!
See commit 859b7f1, commit 42ebeb9(08 Feb 2017) by Linus Torvalds (torvalds
).
(Merged by Junio C Hamano -- gitster
--in commit 015fba3, 27 Feb 2017)
请参阅Linus Torvalds ( ) 的commit 859b7f1和commit 42ebeb9(2017 年 2 月 8 日)。(由Junio C Hamano合并-- --在commit 015fba3,2017 年 2 月 27 日)torvalds
gitster
pathspec magic: add '
^
' as alias for '!
'The choice of '
!
' for a negative pathspec ends up not only not matching what we do for revisions, it's also a horrible character for shell expansion since it needs quoting.
pathspec法宝:添加“
^
”作为别名“!
”
!
为负路径规范选择 ' ' 最终不仅与我们为修订所做的不匹配,而且对于 shell 扩展来说也是一个可怕的字符,因为它需要引用。
So add '^
' as an alternative alias for an excluding pathspec entry.
因此,添加“ ^
”作为排除路径规范条目的替代别名。
回答by l0b0
tl;dr: shopt -s extglob && git log !(unwanted/glob|another/unwanted/glob)
tl;博士: shopt -s extglob && git log !(unwanted/glob|another/unwanted/glob)
If you're using Bashyou should be able to use the extended globbingfeature to get only the files you need:
如果您使用Bash,您应该能够使用扩展的 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 <[email protected]>
Date: Wed Jun 5 10:58:45 2013 +0200
First commit
You can combine this with globstar
for recursive action.
您可以将其与globstar
递归操作结合使用。
回答by rubysolo
You can temporarilyignore the changes in a file with:
您可以暂时忽略文件中的更改:
git update-index --skip-worktree path/to/file
Going forward, all changes to those files will be ignored by git status
, git commit -a
, etc. When you're ready to commit those files, just reverse it:
展望未来,对这些文件的所有更改都将被忽略git status
,git commit -a
等等。当你准备好了提交这些文件,将它取反:
git update-index --no-skip-worktree path/to/file
and commit as normal.
并正常提交。