<p>If you check the <a href="https://wiki.jenkins.io/display/JENKINS/Git+Plugin#GitPlugin-Environmentvariables" rel="nofollow noreferrer">Git plugin documentation</a>, you can see that there is a <code>GIT_BRANCH</code> environment variable that you can use in your pipeline.</p>

<pre><code>GIT_BRANCH - Name of the remote repository (defaults to origin),
             followed by name of the branch currently being used,
             e.g. "origin/master" or "origin/foo"
</code></pre>

<p>So, in your pipeline:</p>

<pre class="lang-java prettyprint-override"><code>stage('Push to Bitbucket') {
    steps {
        withCredentials([usernamePassword(credentialsId: '8e2ca0a6-2dc5-4f89-b76a-03076d6b0843',
                                          passwordVariable: 'GIT_PASSWORD',
                                          usernameVariable: 'GIT_USERNAME')]) {
            sh '''
            # Content omitted
            git checkout ${GIT_BRANCH}
            # Content omitted
            '''
        }
    }
}
</code></pre>

CC BY-SA 4.0
2019-10-25T08:04:04.837Z
2019-10-25T10:31:21.480Z
2019-10-25T10:31:21.480Z
Hedi Nasr
<p>Note that if you're not actually checking out a Jenkinsfile (if you're coding in the Pipeline Script config window), <code>GIT_BRANCH</code> will be <code>null</code>, and Hedi Nasr's answer will fail (you can access the var with <code>${env.GIT_BRANCH}</code> instead to avoid the hard fail, but it will still be <code>null</code>).</p>

<p>You don't give any information about how you checked out the project, so I'm going to assume you cloned it at some point during the execution of your Jenkinsfile. In that case, the repository will be in a <a href="https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt-emgitcheckoutem--detachltbranchgt" rel="nofollow noreferrer">detached state</a>, even if you cloned it with a target branch:</p>

<pre><code>git status
# HEAD detached at 9a92344
</code></pre>

<p>In this case before you can commit you need to checkout your branch as Hedi Nasr points out. You should know your target branch in advance, and can inject it with <a href="https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin" rel="nofollow noreferrer">envInject</a>'s Properties Content if you need to vary by instance.</p>

<p>You can also discover branches with:</p>

<pre><code>git branch -a
</code></pre>

CC BY-SA 4.0
2020-01-13T23:52:55.183Z
2020-01-13T23:58:28.747Z
2020-01-13T23:58:28.747Z
modle13
<p>I want to get git current checkout BRANCH NAME in my Jenkins file here is the code snippet... 
Here I am trying to push a file to my current branch 
I have tried many things could not find it... </p>

<pre><code>stage('Push yaml file to bitbucket'){
  steps{
    withCredentials([usernamePassword(credentialsId: '8e2ca0a6-2dc5-4f89-b76a-03076d6b0843', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
      sh '''
        echo '#!/bin/sh' &gt; /home/jenkins/git_askpass.sh
        echo "exec echo \"${GIT_PASSWORD}\"" &gt; /home/jenkins/git_askpass.sh
        chmod 775 /home/jenkins/git_askpass.sh
        echo "user: ${GIT_USERNAME}"
        GIT_ASKPASS=/home/jenkins/git_askpass.sh
        export GIT_ASKPASS
        cat /home/jenkins/git_askpass.sh
        pwd
        git status
        git checkout feature/OSCCX-44(here I want branch name )
        git config --global user.email "xyz.com"
        git config --global user.name "xyz"
        git add yaml_dev/04-idm-frontend-depl.yaml                
        git commit -m "jenkins commit"
        git push https://${GIT_USERNAME}@bitbucket/repository '''
    }
  }
}
</code></pre>

<git><jenkinsfile><bitbucket>
JenkinsFile to get git branch name
Madan
# JenkinsFile to get git branch name

(Asked by: username_0 on 2019-10-24)

I want to get git current checkout BRANCH NAME in my Jenkins file here is the code snippet... 
Here I am trying to push a file to my current branch 
I have tried many things could not find it... 
stage('Push yaml file to bitbucket'){
  steps{
    withCredentials([usernamePassword(credentialsId: '8e2ca0a6-2dc5-4f89-b76a-03076d6b0843', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
      sh '''
        echo '#!/bin/sh' > /home/jenkins/git_askpass.sh
        echo "exec echo \"${GIT_PASSWORD}\"" > /home/jenkins/git_askpass.sh
        chmod 775 /home/jenkins/git_askpass.sh
        echo "user: ${GIT_USERNAME}"
        GIT_ASKPASS=/home/jenkins/git_askpass.sh
        export GIT_ASKPASS
        cat /home/jenkins/git_askpass.sh
        pwd
        git status
        git checkout feature/OSCCX-44(here I want branch name )
        git config --global user.email "xyz.com"
        git config --global user.name "xyz"
        git add yaml_dev/04-idm-frontend-depl.yaml                
        git commit -m "jenkins commit"
        git push https://${GIT_USERNAME}@bitbucket/repository '''
    }
  }
}

**Tags:** git, jenkinsfile, bitbucket


---

## 2 answers

### Answer by username_1 on 2019-10-25. Score: 2

If you check the Git plugin documentation, you can see that there is a GIT_BRANCH environment variable that you can use in your pipeline.
GIT_BRANCH - Name of the remote repository (defaults to origin),
             followed by name of the branch currently being used,
             e.g. "origin/master" or "origin/foo"

So, in your pipeline:
stage('Push to Bitbucket') {
    steps {
        withCredentials([usernamePassword(credentialsId: '8e2ca0a6-2dc5-4f89-b76a-03076d6b0843',
                                          passwordVariable: 'GIT_PASSWORD',
                                          usernameVariable: 'GIT_USERNAME')]) {
            sh '''
            # Content omitted
            git checkout ${GIT_BRANCH}
            # Content omitted
            '''
        }
    }
}


---

### Answer by username_2 on 2020-01-13. Score: 0

Note that if you're not actually checking out a Jenkinsfile (if you're coding in the Pipeline Script config window), GIT_BRANCH will be null, and username_1's answer will fail (you can access the var with ${env.GIT_BRANCH} instead to avoid the hard fail, but it will still be null).
You don't give any information about how you checked out the project, so I'm going to assume you cloned it at some point during the execution of your Jenkinsfile. In that case, the repository will be in a detached state, even if you cloned it with a target branch:
git status
# HEAD detached at 9a92344

In this case before you can commit you need to checkout your branch as username_1 points out. You should know your target branch in advance, and can inject it with envInject's Properties Content if you need to vary by instance.
You can also discover branches with:
git branch -a


---

