Junior — Middle
Can you explain what hooks are in the Git version control system and how they are used?
sobes.tech AI
Answer from AI
In Git, hooks are scripts that automatically execute when certain events occur in the repository, such as before a commit or after receiving changes.
They are used to automate tasks such as:
- Checking code before committing (pre-commit), for example, running tests or linters.
- Automatic code formatting.
- Sending notifications after a push (post-push).
An example of a simple pre-commit hook that prevents committing if there is no description in the message:
#!/bin/sh
commit_msg_file=$1
if ! grep -q '.' "$commit_msg_file"; then
echo "Error: commit message must not be empty."
exit 1
fi
This script is placed in .git/hooks/pre-commit and must be executable.