Git pre-commit hooks can be used to enforce version control best practices.
If your development process is like mine, it includes three stages of maturity: develop, staging and production. The first two stages are represented by the identically named Git branches ‘develop’ and ‘staging’, whereas production is represented by the ‘master’ branch.
My standard workflow implies that all commits are done in the ‘develop’ branch. When a certain level of maturity is reached, the whole branch is merged up to ‘staging’. When ‘staging’ has proven its production-readiness, the code is merged up to ‘master’.
The following script ensures that commits are only done in the ‘develop’ branch, if such a branch exists, because that’s where the development takes place. It still allows to perform commits in other branches, if that is deemed necessary, to enable hotfixes e.g. This can only be done when using the CLI though. When using an IDE overriding is not possible and and an appropriate message is issued (tested in IntelliJ IDEA only).
In order for this script to be called upon every use of git commit
it must
be executable and be registered as a global Git pre-commit hook. To do that
it has to be named pre-commit
and placed in a directory which has to be
registered as the global hook directory: