title: Using Jenkins declarative pipeline, how do I fetch and compare another branch with a private github repo?
body: <h1>Background</h1>
  
  <p>My team wants to update several linting rules in our project, however, doing so will cause our Jenkins build pipeline which lints, tests and builds each feature branch to break.  We don't want to lose the value of linting each feature branch before merging, so we agree that linting only the files that the feature branch changes is a reasonable way to introduce these new lint rules without forcing us to re-lint the whole project up-front.  Given that our entire project is already linted, this seems like a reasonable move. </p>
  
  <p>A while ago I wrote a git tool to do exactly this.  It determines which files have changed since the feature branch diverged from <code>master</code> and outputs those files so they can be consumed by <code>eslint</code>, <code>pycodestyle</code> and other linters.  Here's <a href="https://github.com/codeocelot/git-diff-lint" rel="nofollow noreferrer">the source</a> if you're interested in how this is done.</p>
  
  <h1>Problem</h1>
  
  <p>Jenkins declarative build process and it's GitHub Branch Source Plugin seem to have a brittle checkout behavior that can't be modified to checkout more than just the feature branch that it's called on to build.</p>
  
  <p>If I call <code>git fetch origin stage</code> within a build step, Jenkins complains about missing credentials.  I don't feel comfortable sticking in credentials into my pipeline file, I'd MUCH prefer to continue using the Git plugin to manage credentials to our private github repo and pull branches, however, I'm at a loss as to how to specify for it to fetch more than just the feature branch. </p>
  
  <p>For reference, here's the relevant portions of my Jenkinsfile </p>
  
  <p>As you can see, I've tried adding the GitSCM code block to no avail. I've read this <a href="https://medium.com/rocket-travel-engineering/running-advanced-git-commands-in-a-declarative-multibranch-jenkinsfile-e82b075dbc53" rel="nofollow noreferrer">medium article</a> which solves a similar problem, but I'm not using SSH credentials and I'd prefer not to given than we're already managing credentials using the Git plugin.</p>
  
  <pre><code>pipeline {
    agent any
    tools {
      nodejs 'node12.7.0'
    }
    stages {
      stage('checkout') {
        steps {
          checkout([
            $class: 'GitSCM',
            branches: [[name: '*']],
            extensions: scm.extensions,
            userRemoteConfigs: [],
            doGenerateSubmoduleConfigurations: true
          ])
        }
      }
      stage('install') {
        steps {
          script {
            sh 'git config --add remote.origin.fetch +refs/heads/master:refs/remotes/origin/master'
            sh 'yarn install'
          }
        }
      }
      stage('lint &amp; test') {
        failFast true
        parallel {
          stage('lint') {
            when {
              not {
                anyOf {
                  branch 'stage'; branch 'int'; branch 'prod'
                }
              }
            }
            steps {
              script {
                sh """
                  git submodule update --init
                  yarn run lint
                """
              }
            }
          }
          ...
        }
      }
      stage('deploy') {...}
      }
    }
    post {
      failure {
        notifySlack()
      }
    }
  }
  </code></pre>