title: Jenkinsfile: git rev-parse --abbrev-ref HEAD returns HEAD
body: <p>I am new to jenkins/devops; I am following <a href="https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/#git_branch" rel="noreferrer">this example</a>. When I locally do (from the terminal):</p>
  
  <pre><code>git rev-parse --abbrev-ref HEAD
  </code></pre>
  
  <p>I get the current branch's name. However from within Jenkinsfile, in the logs I am getting:</p>
  
  <pre><code>HEAD
  </code></pre>
  
  <p>Been researching online for a while and couldn't find the reason so far. What are potential causes for this outcome?</p>
  
  <h2>Additional Details</h2>
  
  <p>In my jenkinsfile, i am trying to get the current git branch's name (the one that triggered the webhook) and then pipe it inside 'git branch' command, so code is as follows:</p>
  
  <pre><code>pipeline {
  agent {
      label 'ubuntu'
  }
  stages {
      stage('check') {
      steps {
              script {
                 env.GIT_BRANCH_NAME=sh(returnStdout: true, script: "git rev-parse --abbrev-ref HEAD").trim()
              }
  
              sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}'
              git branch: GIT_BRANCH_NAME, credentialsId: '******', url: 'https://*****/*****/*****.git'
        }
  ....
  }
  </code></pre>
  
  <p>In the line</p>
  
  <pre><code>sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}'
  </code></pre>
  
  <p>Returns <b>HEAD</b></p>
  
  <p>I found a way around this using <b>git name-rev --name-only HEAD</b> and modified the script code to: </p>
  
  <pre><code>script {
      env.GIT_BRANCH_PATH=sh(returnStdout: true, script: "git name-rev --name-only HEAD").trim()
      env.GIT_BRANCH_NAME=GIT_BRANCH_PATH.split('remotes/origin/')[1]
  }
  </code></pre>
  
  <p>Now I get the right branch name and the steps work, but I would rather have a less hacky way of doing things.</p>
  
  <p><b><i>What is the best method to achieve what I want to achieve using best practices?</i></b></p>
  
  <p><b>PS</b> I am not using multi-branching pipeline and the requirements were to not use multi-branching.</p>