How can I prevent composer from detaching a module's head?
mradcliffe is correct:


  composer checks out the git commit hash rather than a branch if I recall correctly.


This is fairly easy to deal with though. For example, if your main branch is 8.x-1.x, then you do some work on detached head, you can commit the work on the detached head, which will give you a hash. You then check out the 8.x-1.x branch, and merge the hash. Since the detached head has the 8.x-1.x branch upstream, it can be merged back into that branch after you make your changes.

Example:

$ git status
HEAD detached at aa284c1
$ git add file.php
$ git commit -m "Committed file.php"
[detached HEAD ef37212] Comitted file.php
$ git checkout 8.x-1.x
$ git merge ef37212


If you realize early enough, you can actually just run git add on all your changed filed, then check out  the 8.x-1.x branch. Then you can continue to work there, or make a commit on that branch. Or you can even create a new branch from 8.x-1.x and continue your work in that branch.