I am always falling into detached head state I don't know why. And still not able to find a good way to get out of it. Some time I was thinking that it is sufficient to create branch when in detached head, but this doesn't help, HEAD remains detached:
>git branch
(HEAD detached at af34e34)
master
>git branch detachedhead20160610
>git branch
(HEAD detached at af34e34)
detachedhead20160610
master
>git branch detachedhead20160610
Fatal: A branch named 'detachedhead20160610' already exists.
>git branch detachedhead20160610-2
>git branch
(HEAD detached at af34e34)
detachedhead20160610
detachedhead20160610-2
master
I.e. new branches just added without curing detached head state.