Correctly resolve HEAD references for both branch checkouts and detached heads (tag or commit)