I canโt believe how difficult it is. I do this for Jenkins too. I put the decision of Peter Kuchinsky:
branch=`git rev-parse HEAD | git branch -a --contains | grep remotes | sed s/.*remotes.origin.//` branch=`echo $branch | awk '{print $NF}'`
Because sometimes, as Matt Cantor noted, Piotraโs decision gives a lot of rubbish. But the last word in this junk always seems to be right. Please note that this solution only works if the ref you are using matches the branch that exists on the remote computer (therefore, the local branches do not work).
source share