
Git Hooks
Git hooks are scripts that Git automatically executes before or after specific events, such as committing changes or pushing to a repository. They allow you to customize and automate tasks related to these events, such as enforcing code style rules, running tests, or sending notifications. Hooks are useful for maintaining code quality and ensuring consistent workflows across a team. They can be set up in the .git/hooks directory of your repository and include both client-side and server-side hooks.
Simple Pre Commit Hook Example
#!/usr/bin/env bash
echo "Running Pre Commit Hook"
hatch test
TEST_RESULT=$?
if [ $TEST_RESULT -ne 0 ]; then
echo "Tests failed. Aborting commit"
exit 1
fi
echo "Tests passed. Committing code"
exit 0
In order to share this with other users of the project, move the hook to a shared folder.
mkdir hooks
cp .git/hooks/pre-commit.sample hooks/pre-commit
Modify the precommit file. Then point the git hooks to the new hooks directory.
git config core.hooksPath hooks
And now run:
git commit -m “testing git commits”
You should get passed or failed based on your project.
Running Pre Commit Hook
Tests passed. Committing code