> For the complete documentation index, see [llms.txt](https://til.yulrizka.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://til.yulrizka.com/git/force-fail-commit-on-master.md).

# force fail commit on master

Most of the time, you want to fail if you mistakenly commit to master. This is if you normally work with feature branch. This script can make git commit fail if the branch is master

add to `.git/hooks/pre-commit`

```
 current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
 if [ "$current_branch" == "master" ]
 then
   echo "Should not commit on master branch. Use 'git commit -n' to override"
   exit 1
 fi
```

or you can check out [pre-commit](https://github.com/pre-commit/pre-commit) a framework to manage your git commit scripts
