[git] IDEA-134286 Fix reading detached HEAD state from .git

The branch pattern is so tolerant, that it matches the hash as well
=> at first try to match the hash.

Add a test for the detached HEAD state.