0
0
Gitdevops~5 mins

pre-commit hook in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
A pre-commit hook is a small script that runs automatically before you save changes to your code repository. It helps catch mistakes early by checking your code or files before they are saved, so you avoid committing errors.
When you want to check code style or formatting before saving changes.
When you want to run tests automatically before committing code.
When you want to prevent committing secrets or sensitive data by mistake.
When you want to ensure commit messages follow a specific format.
When you want to stop commits that break basic rules or guidelines.
Config File - .git/hooks/pre-commit
.git/hooks/pre-commit
#!/bin/sh
# Simple pre-commit hook to check for trailing whitespace

if git diff --cached --check | grep -q 'trailing whitespace'; then
  echo 'Error: Trailing whitespace found. Please remove it before committing.'
  exit 1
fi

exit 0

This script runs before a commit is saved.

It checks the staged changes for trailing whitespace using git diff --cached --check.

If trailing whitespace is found, it prints an error message and stops the commit with exit 1.

If no issues are found, it allows the commit to proceed with exit 0.

Commands
Make the pre-commit hook script executable so Git can run it automatically before commits.
Terminal
chmod +x .git/hooks/pre-commit
Expected OutputExpected
No output (command runs silently)
Stage the file 'example.txt' to prepare it for commit. The pre-commit hook will check this staged content.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Attempt to commit the staged changes. The pre-commit hook runs now and checks for trailing whitespace.
Terminal
git commit -m "Add example file with trailing spaces"
Expected OutputExpected
Error: Trailing whitespace found. Please remove it before committing.
Manually run the check that the pre-commit hook uses to find trailing whitespace in staged files.
Terminal
git diff --cached --check
Expected OutputExpected
example.txt:3: trailing whitespace.
Key Concept

If you remember nothing else from this pattern, remember: a pre-commit hook automatically checks your staged changes and can stop bad commits before they happen.

Common Mistakes
Not making the pre-commit hook script executable.
Git will not run the hook if it does not have execute permission, so the checks won't happen.
Run 'chmod +x .git/hooks/pre-commit' to make the script executable.
Writing the hook script with syntax errors or wrong commands.
The hook will fail silently or cause errors, preventing commits or not checking properly.
Test the script manually and ensure it runs correctly in the shell before using it as a hook.
Expecting the hook to check unstaged changes.
Pre-commit hooks only check staged changes, so unstaged errors won't be caught.
Always stage your changes with 'git add' before committing to have them checked.
Summary
Create a pre-commit hook script in .git/hooks/pre-commit to run checks before commits.
Make the script executable with chmod +x so Git can run it automatically.
Stage your changes and commit; the hook will run and stop commits if problems are found.