Git Hooks - The Magical Helpers of Your Codebase
Shruti Yadav
SDE-2 @Salesforce| Mentor | Author of Shruti’s Sphere | NIT Bhopal ‘21
Concept of Git Hooks explained in a playful manner
We as developers cherish bug free codes, smooth code reviews and beautiful git history and logs to look at. We can’t help but fall in love with the developer who submits clean, well-organized changelists and consistent, clear commit messages. Let's be honest, we all do. This raises a question: how to get someone fall in love with our code review changelist, pull request messages and commits. Well who doesn’t like to be loved. I absolutely do.
This is where git hooks play a part in making our code reviews fall for us without even lifting a finger. Okay enough of the praise of git hooks. Let’s dive into what git hooks are. Are they even worth this praise or am I crazy:) Intrigued? Let me a weave you a little story. Keep reading.
Meet the git hooks
Once upon a time, in the kingdom of Codeville, there was a bustling city called Git Town. This town was where all the coders lived, working day and night to create beautiful, bug-free code. Now, Git Town had some magical helpers called Git Hooks. These hooks were tiny, invisible creatures with a special job: to make sure everything ran smoothly in the land of code. Git Hooks are like the secret agents of your codebase. They jump into action at specific points in your Git workflow, ensuring everything goes according to plan. Imagine them as tiny guardians who help you catch mistakes, automate tasks, and enforce rules.
Now there are different types of these little secret agents. Let’s explore few of them with their roles in code review process by considering that every piece of code passes through various gates before it is reviewed by the wise code reviewers.
Pre-commit hook: First line of defence
# Example: A pre-commit hook to run a linter
# .git/hooks/pre-commit
#!/bin/bash
echo "Running linter..."
eslint . || { echo "Linting failed! Please fix errors."; exit 1; }
Commit-Msg hook: The herald of clarity
# Example: A commit-msg hook to enforce a commit message format
# .git/hooks/commit-msg
#!/bin/bash
commit_msg=$(cat "$1")
if ! grep -qE "^[A-Z0-9-]+: .+" <<< "$commit_msg"; then
echo "Commit message must start with a ticket number followed by a colon."
exit 1
fi
Pre-push hook: The border Patrol
# Example: A pre-push hook to run tests
# .git/hooks/pre-push
#!/bin/bash
echo "Running tests before push..."
if ! ./run_tests.sh; then
echo "Tests failed! Push aborted."
exit 1
fi
Pre-receive hook: The final gatekeeper
These hooks help reviewers to focus on more critical aspects of code like logic, functionality and design through maintaining consistency, efficiency and quality.
So after all, git hooks are the unsung heroes working behind the scenes making the kingdom of codeville a place of order and excellence. By recognizing these heroes, the developers in the kingdom live happily ever after.
If you want your code reviewer to fall in love with you ( trust me, they will ), use git hooks. You will be helping yourself as well as others in the process.
Hope you liked the story and the secret to love ;)
Please subscribe, like and share if you enjoyed the tale of our agents ( git hooks). Be honest, you would watch this movie, right?
I appreciate all the feedback and support.
Yours in discovery,
Shruti