Is it 开发者_JAVA技巧somehow possible to ignore certain files during git diff? I'm specifically interested in filtering out Makefile.in, Makefile, configure (and all that auto-generated crap).
You can try passing a list of files to git diff
, and it will execute the diff only on those files.
Depending on how your code is setup, it can be as easy as:
git diff otherbranchname -- src/ # only give me diffs from src/
or as unsightly/complex as:
git diff otherbranchname -- $( git ls-files |
perl -lne'next if /(?:[mM]akefile(?:\.in)?|configure|whateverelse)/;print' )
# diff all files which don't match the pattern
Depending on whether the files you want to ignore are managed by Git in the current branch or not, you'll need to replace the git ls-files
with a find *
; use instead a find .
and add |.git
to the Perl expression.
Tailor this as you see fit, possibly using the directories / file names you know you'd like to diff, rather than using the git ls-files
or find
.
Another similar solution (add to .git/config
):
[alias]
mydiff = !git diff -- $(git diff --name-only | grep -Ev "([mM]akefile|configure)")
Then run it with:
git mydiff
Note that git diff --name-only
is better than git ls-files
because it will pass a shorter list of files to git diff
, since only files that are modified will be included. I've run into trouble with exceeding the maximum number of arguments in large projects when using git ls-files
.
Regarding gitignore and autotools integration, you might be interested in git.mk: http://mail.gnome.org/archives/desktop-devel-list/2009-April/msg00211.html
As detailed here you can use an exclude
pathspec magic
git diff -- . ':(exclude)Makefile*' ':(exclude)configure'
If you want to ignore the content of the diffs but still do care whether the files have changed, this is a job for .gitattributes
and the diff
attribute. Say you have a directory structure like
repo
repo/foo
repo/foo/Makefile
repo/bar
repo/bar/Makefile.in
repo/bar/baz
repo/bar/baz/Makefile.in
If you want to ignore all three Makefiles, create repo/.gitattributes
as:
Makefile* -diff
If you want to ignore bar/Makefile.in
but not the others, create repo/bar/.gitattributes
as
Makefile.in -diff
but also repo/bar/baz/.gitattributes
as
Makefile.in diff
For more detail of what can be done with .gitattributes
, see the docs; my examples are drawn from theirs, made specific to diff.
(h/t to user3589608's answer here)
精彩评论