Sort the branch list used to rebuild the last commit

Hi,

This patch sorts the branch list used to rebuild the last commit. It also filters out */HEAD from the branch list if more than one branch exists, so GIT_BRANCH can never be equals to */HEAD if another branch is also built. This fixes JENKINS-17150. As we use GIT_BRANCH to route built artifact to different repositories, having */HEAD as a value doesn't make sense.

Sorting the branch list ensures a deterministic value for GIT_BRANCH and allows a git-flow style workflow with fast-forward merge between branches.

It's written on top of 1.x branch but merges successfully on master.