title: Jenkins - build specific commit on specific branch
body: <p>in a jenkinsfile I have a declarative pipeline to build two Maven projects consecutively checked out from the Github repositories</p>
  <p>repo2 (<a href="https://github.com/MaxHoefl/repo2" rel="nofollow noreferrer">https://github.com/MaxHoefl/repo2</a>)</p>
  <p>repo1 (<a href="https://github.com/MaxHoefl/repo1" rel="nofollow noreferrer">https://github.com/MaxHoefl/repo1</a>)</p>
  <h2><strong>Problem:</strong></h2>
  <p>How can I checkout a Github repo on a specific branch and even a specific commit on that branch?</p>
  <h2>Jenkinsfile</h2>
  <pre><code>pipeline {
  agent any
  
  tools {
      maven &quot;localMaven&quot;
      jdk &quot;localJDK&quot;
      git &quot;localGit&quot;
  }
  
  stages {
      stage('Checkout-Repo2') {
          steps {
              git 'https://github.com/MaxHoefl/repo2.git'
              sh &quot;echo {$env.GIT_URL}&quot;
          }
      }
      
      stage('Build-Repo2') {
          steps {
              sh &quot;mvn clean install&quot;
              archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true 
          }
      }
      
      stage('Checkout-Repo1') {
          steps {
              git 'https://github.com/MaxHoefl/repo1.git'
              sh &quot;echo {$env.GIT_URL}&quot;
          }           
      }
      
      stage('Build-Repo1') {
          steps {             
              sh &quot;mvn clean install&quot;
          }   
      }
  }
  
  post {
      success {
        echo &quot;Build ID:    {$env.BUILD_ID}&quot;
        echo &quot;Jenkins URL: {$env.JENKINS_URL}&quot;
        echo &quot;Jobname:     {$env.JOB_NAME}&quot;
        echo &quot;Branch:      {$env.BRANCH_NAME}&quot; //null
        echo &quot;Change id:   {$env.CHANGE_ID}&quot;
      }
      
      failure {
        
      }
      
      always {
          echo 'Cleaning up....'
          deleteDir()
      }
    }
  }
  </code></pre>
  <p>Thanks a lot!</p>
  <h2>Edit 1</h2>
  <p>Instead of using <code>git</code> command</p>
  <pre><code>stage('Checkout-Repo2') {
          steps {
              git 'https://github.com/MaxHoefl/repo2.git'
          }           
      }
  </code></pre>
  <p>I tried using the more general <code>checkout</code> command</p>
  <pre><code>stage('Checkout-Repo2') {
          steps {
              checkout([$class: 'GitSCM', branches: [[name: ''a5e1ddd4e45aeb396b44beece370f891bd0c7fe5'']],
                        userRemoteConfigs: [[url: 'https://github.com/MaxHoefl/repo2.git']]])
          }
      }
  </code></pre>
  <p>This works. Will try to also include the branch name</p>