0
0
Gitdevops~10 mins

pre-commit hook in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - pre-commit hook
Make code changes
Run 'git commit'
Git triggers pre-commit hook script
Hook script runs checks
Commit
Fix issues and retry
When you run 'git commit', Git runs the pre-commit hook script first. If the script passes, the commit happens. If it fails, the commit stops so you can fix problems.
Execution Sample
Git
#!/bin/sh
# pre-commit hook example

# Check for TODO comments
if git diff --cached | grep -q 'TODO'; then
  echo 'Error: TODO found in staged files.'
  exit 1
fi
exit 0
This pre-commit hook checks if any staged files contain 'TODO'. If yes, it stops the commit with an error.
Process Table
StepActionCommand/CheckResultNext Step
1User runs 'git commit'git commitTriggers pre-commit hookRun hook script
2Hook script runs checkgit diff --cached | grep 'TODO'No TODO foundAllow commit
3Commit proceedsgit commit completesCommit savedEnd
4User runs 'git commit' againgit commitTriggers pre-commit hookRun hook script
5Hook script runs checkgit diff --cached | grep 'TODO'TODO foundAbort commit
6Commit abortedexit 1Commit stoppedUser fixes TODO and retries
💡 Commit stops if TODO found; otherwise commit completes.
Status Tracker
VariableStartAfter Step 2After Step 5Final
TODO_foundundefinedfalsetruetrue
Commit_statusnot startedallowedabortedaborted or completed
Key Moments - 3 Insights
Why does the commit stop even though I typed 'git commit'?
The pre-commit hook script found a TODO in staged files (see execution_table step 5), so it exited with error to prevent committing unfinished code.
What happens if the pre-commit hook script passes all checks?
If no problems are found (execution_table step 2), the commit proceeds normally (step 3) and your changes are saved.
How can I fix a failed pre-commit hook?
Fix the issues reported by the hook (like removing TODO comments), stage the changes again, and run 'git commit' to retry (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of the hook check at step 2?
ATODO found, commit aborted
BNo TODO found, commit allowed
CHook script did not run
DCommit completed
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the commit get aborted due to a failed hook?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look for 'TODO found' and 'Abort commit' in the execution_table.
If the hook script always exits with 0, what will happen to the commit?
ACommit will always proceed
BHook script will not run
CCommit will always be aborted
DGit will show an error
💡 Hint
Refer to the exit codes in the execution_table and how they affect commit status.
Concept Snapshot
pre-commit hook is a script Git runs before saving a commit.
It can check code or files and stop commit if problems found.
Place script in .git/hooks/pre-commit and make executable.
Exit 0 means pass; exit non-zero means fail and abort commit.
Useful to enforce code quality automatically.
Full Transcript
When you run 'git commit', Git first runs the pre-commit hook script if it exists. This script can check your staged files for issues like TODO comments. If the script finds a problem, it exits with an error code, stopping the commit so you can fix it. If no problems are found, the commit proceeds normally. This helps keep your code clean by preventing commits with unfinished or problematic code. You can create a pre-commit hook by adding an executable script named 'pre-commit' in the .git/hooks directory. The script should exit with 0 to allow commit or non-zero to stop it.