Commit on detached HEAD in Git

First of all, don't commit on detached Head.

A "detached head" in git isn't the same as Mercurial's concept of a "head". Git has exactly one HEAD, which is just the currently checked-out commit. "Detached" just means that you don't currently have a branch checked out. So when you create a commit, it won't be associated with a branch, and it'll be lost when you checkout a different commit.

In Git, every commit that you care about should be reachable from some branch. There's no such thing as an "anonymous branch", which is why git warns you when committing on a detached head. Committing on a detached head is like allocating an object and then throwing away the pointer; it's possible, but almost never what you want to do. Remember, branches in git are lightweight and can be ephemeral. Just create a branch before committing so that you can find your commits again.

But it's too late, how to get it back ?

git reflog

It will show all commit with hash.

And create a new branch with the hash by

git branch <new-branch> <hash>