5

I'm using a Git pre-commit hook to check commits. The pre-commit script basically does one thing:

exec git diff-index --check --cached HEAD -- 

It does some other things too, but they are irrelevant for this discussion.

The problem is, I have all sorts of files in the repository, and not all of them have to comply with the checks that enforced by "git diff-index --check".

How can I exclude/ignore these files? That is, I do track them in Git, but I want to ignore them in pre-commit check only.

For instance, a certain patch contains *.c, *.h, *ini, and *.xyz files. I want the "git diff-index --check" to apply to .c and .h files only.

2
  • 1
    The manual page says: "When <path> arguments are present, compares only paths matching those patterns. Otherwise all tracked files are compared." So, add '*.c' '*.h' when you want those checked, whenever that is. Commented Oct 8, 2013 at 16:09
  • @torek Indeed, your suggestion works :) I guess the "path" argument name is misleading a bit - it made me thing that this is about path, when in fact it is all about file name patterns, including path. Please post your thing as an answer. Commented Oct 8, 2013 at 18:59

2 Answers 2

8

The man page says:

When <path> arguments are present, compares only paths matching those patterns. Otherwise all tracked files are compared.

In other words, the "path" arguments are really glob-style patterns, not just specific paths. You can just add '*.c' '*.h' to your command.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure if it's an issue with my env (Windows + Git bash) or with different git version, but only the following worked for me (no quotes that is): git diff-index --check --cached HEAD -- *.html *.js
@Mr_and_Mrs_D: I think it should. I have not tested it. All pathspec handling should be done through common code in Git, so that it all behaves the same (but it's not actually done that way: in particular, gitignore pathspecs use separate code).
2

The accepted answer deals correctly with the exclusion of filename extensions. To exclude files residing in a directory, you can use the following command.

git diff-index --check --cached HEAD -- ':!directory' 

1 Comment

How can I add more directories ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.