Fix get git version

Previous code only worked when at the tip of a branch, now it also handles detached HEAD states.