Jenkins: GitSCM finishes the clone in a detached head state, how can I make sure that the correct branch name is checked out?

After lots of researching and even contacting Jenkins professionals and many tries from my side, I found how to solve this issue.

The following code fixes the issue:

checkout([$class: 'GitSCM', branches: [[name: '*/feature/*']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '99f978af-XXXX-XXXX-8147-2cf8f69ef864', url: 'http://TFS_SERVER:8080/tfs/DefaultCollection/Product/_git/Project']]])

Notice the "**" in the localBranch extension.

Jenkins log:

Checking out Revision 97b3493db4f726e11e33XXXba34fa808b63edec5 (origin/feature/merge_tfs)
 > git.exe config core.sparsecheckout # timeout=10
 > git.exe checkout -f 97b3493db4f726e11e33XXXba34fa808b63edec5
 > git.exe branch -a -v --no-abbrev # timeout=10
 > git.exe checkout -b feature/merge_tfs 97b3493db4f726e11e33XXXba34fa808b63edec5
 > git.exe rev-list 97b3493db4f726e11e334e5ba34fa808b63edec5 # timeout=10
[Pipeline] bat
[Ensure] Running batch script

C:\Program Files (x86)\Jenkins\workspace\Ensure>cd C:\Program Files (x86)\Jenkins\workspace\Ensure 

C:\Program Files (x86)\Jenkins\workspace\Ensure>git branch 
* feature/merge_tfs

Adding [[$class: 'LocalBranch', localBranch: "**"]] fixed my issue.

After a successful checkout of master branch using GitSCM plugin, .git/refs/heads/master was still missing making some operations to always fail (e.g. git log --decorate ... to create changelog).

Tags:

Git

Jenkins