title: How to find the current git branch in detached HEAD state
body: <p>I can find the current git branch name by doing either of these:</p>
  
  <pre><code>git branch | awk '/^\*/ { print $2 }'
  git describe --contains --all HEAD
  </code></pre>
  
  <p>But when in a detached HEAD state, such as in the post build phase in a <a href="http://jenkins-ci.org/" rel="noreferrer">Jenkins</a> maven build (or in a Travis git fetch), these commands doesn't work.</p>
  
  <p>My current working solution is this:</p>
  
  <pre><code>git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)|\1|' | sort -u | grep -v HEAD
  </code></pre>
  
  <p>It displays any branch name that has the last commit on its HEAD tip.  This works fine, but I feel that someone with stronger git-fu might have a prettier solution?</p>