title: How can I run git pre-commit checks only on staged content?
body: <p>Suppose <code>git status</code> gives this:</p>
  
  <pre><code># On branch X
  # Changes to be committed:
  #   (use "git reset HEAD &lt;file&gt;..." to unstage)
  #
  #   modified:   file1.cc
  #   modified:   file1.h
  #   modified:   file1_test.cc
  #   modified:   SConscript
  #
  # Changes not staged for commit:
  #   (use "git add &lt;file&gt;..." to update what will be committed)
  #   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
  #   (commit or discard the untracked or modified content in submodules)
  #
  #   modified:   file1.cc
  #   modified:   tinyxml2 (untracked content)
  #
  </code></pre>
  
  <p>In this case, <strong>only some of the changes made to file1.cc have been staged</strong>/indexed for the next commit.</p>
  
  <p>I run a pre-commit script to do run a style checker:</p>
  
  <pre><code>#!/bin/bash                                                                                                                                                                     
  
  git stash -q --keep-index
  
  # Do the checks                                                                                                                                                                 
  RESULT=0
  while read status file
  do
      if python ~/python/cpplint.py "$file"; then
          let RESULT=1
      fi
  done &lt; &lt;(git diff --cached --name-status --diff-filter=ACM | grep -P  '\.((cc)|(h)|(cpp)|(c))$' )
  
  git stash pop -q
  
  [ $RESULT -ne 0 ] &amp;&amp; exit 1
  exit 0
  </code></pre>
  
  <p>As suggested by <a href="http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/" rel="noreferrer">here</a>, I stash unstaged files before I run my style checks, and pop them afterwards. However, in the case where only some of the changes in a file were staged, this causes a merge conflict when I pop the stash at the end of the pre-commit hook.</p>
  
  <p>What is a better way to do this? I want to run a style check against the staged version of the files about to be committed.</p>