Why does git allow you to commit to a detached head? Is there any pre commit hook that can disable it? What is the purpose? Many new developers do this and I'd like to find a way to disable it.
This can be only prevented by a local git pre-commit
hook, so developers would need to create it. Add the your-local-project/.git/hooks/pre-commit
file with the following contents:
#!/bin/sh
if ! git symbolic-ref HEAD &> /dev/null; then
echo "You are in a detached head state! Commit has been blocked. (Use --no-verify to bypass this check.)"
exit 1
fi
Make sure it's executable. Credits go to svachalek
Why should git prevent commiting in detached HEAD? Detached HEAD means only that there is no pointer to the repository state you are working on. It assumes that you know what you are doing.
I would rather investigate why many developers in your team enter this state? Maybe they apply some weird worklow?