[FIXED JENKINS-17150] Filter out */HEAD from candidate branch list if it's not the only branch
Sort the branch list used to rebuild the last revision if there's no other candidate rev to build. It ensures a deterministic value for GIT_BRANCH and allows a git-flow style workflow with fast-forward merges between branches