How to run 'git diff –check' on certain file types only

I have been unable to figure out how to run ‘git diff –check’ on only Python files in my commit. I have a commit with 3 files in it:

$ git show --name-only
...
tools/gitHooks/preReceive.py
tools/gitHooks/unittests/testPreReceive.py
Makefile

The last 2 files have trailing whitespace in them.

  • Is there a way to just commit one file type extension from a folder?
  • git grep by file extensions
  • How to git grep only a set of file extentions
  • Configure git to track only one file extension
  • For a .Net project, what file extensions should I exclude from source control?
  • What option should be used restrict the git diff to a given set of file extensions?
  • $ git diff --check HEAD~1 HEAD
    tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
    +newmock = Mock()
    Makefile:41: new blank line at EOF.
    

    I would like to restrict the whitespace check to only Python files. Doing this gives me the correct files:

    $ git diff --name-only HEAD~1 HEAD | grep ".py"
    tools/gitHooks/preReceive.py
    tools/gitHooks/unittests/testPreReceive.py
    

    But when I pipe the filenames to ‘git diff –check’ (with or without xargs) I get the wrong output.

    $ git diff --name-only HEAD~1 HEAD | grep ".py" | xargs git diff --check HEAD~1 HEAD --
    $
    $ git diff --name-only HEAD~1 HEAD | grep ".py" | git diff --check HEAD~1 HEAD --
    tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
    +newmock = Mock()
    Makefile:41: new blank line at EOF.
    

    I also tried this as well as a few variations on it with no luck:

    $ git diff --check HEAD~1 HEAD -- "*.py"
    $
    

    Can anyone help? I feel like I’m close to the answer.

  • bundle install rails “sh.exe has stopped working” on windows 7
  • Git diff tool over several commits with other's commit inbetween
  • How to synchronise remote branches with trunk using git-svn
  • Organize LabVIEW in a git repository
  • Is it possible to have a trailing period in user.name in Git?
  • Remove deleted files from github pull request?
  • One Solution collect form web for “How to run 'git diff –check' on certain file types only”

    From the original poster:

    Turns out I was using the wrong grep.

    $ git diff --name-only HEAD~1 HEAD | grep \.py$ | \
    $ xargs git diff --check HEAD~1 HEAD --
    tools/gitHooks/unittests/testPreReceive.py:75: trailing whitespace.
    +newmock = Mock()
    
    Git Baby is a git and github fan, let's start git clone.