title: How to exit a git hook script if a git command fails?
body: <p>I have a <code>post-receive</code> git hook:</p>
  
  <pre><code>#!/bin/bash
  
  while read oldrev newrev refname
  do
      branch=$(git rev-parse --symbolic --abbrev-ref $refname)
      if [ -n "$branch" ] &amp;&amp; [ "master" == "$branch" ]; then
         working_tree="/path/to/working/dir"
         GIT_WORK_TREE=$working_tree git checkout $branch -f
         GIT_WORK_TREE=$working_tree git pull
         &lt;more instructions&gt;
      fi
  done
  </code></pre>
  
  <p>How can I check the status of a git command and stop the script from continuing if it fails?</p>
  
  <p>Something like the following:</p>
  
  <pre><code>#!/bin/bash
  
  while read oldrev newrev refname
  do
      branch=$(git rev-parse --symbolic --abbrev-ref $refname)
      if [ -n "$branch" ] &amp;&amp; [ "master" == "$branch" ]; then
         working_tree="/path/to/working/dir"
         GIT_WORK_TREE=$working_tree git checkout $branch -f
         GIT_WORK_TREE=$working_tree git pull
         if [ &lt;error conditional&gt; ]
             echo "error message"
             exit 1
         fi
      fi
  done
  </code></pre>