Hook
- Git course
- Advanced
- Lesson 56/56
- 6 min
Add a pre-commit hook and make it executable.
What Is a Git Hook?
A Git hook is a script that runs automatically before or after specific Git operations. Hook files are located in the repository's .git/hooks directory. For example, a pre-commit hook runs before a commit is created and can perform formatting or simple validation checks.
.git/hooks/pre-commitThe hook file must have executable permissions.
Practice Create a Pre-Commit Hook
Add a Bash script to .git/hooks/pre-commit. When the script runs, it should print Checking commit and return a successful exit code. Specify the Bash interpreter on the first line.
Starter code
# .git/hooks/pre-commit dosyasının içeriğiSample solution
#!/bin/bash
echo "Checking commit"
exit 0Expected output
Checking commit
Practice Make the Hook File Executable
Grant executable permission to the pre-commit file you created. Then use the ls -l command to inspect its permissions. The command sequence must perform the permission change first and the inspection second.
Starter code
# Hook dosyasına çalıştırma izni ver
# Dosya izinlerini görüntüleSample solution
chmod +x .git/hooks/pre-commit
ls -l .git/hooks/pre-commitExpected output
Hook dosyasına çalıştırma izni verildi ve izinleri görüntülendi.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which hook runs immediately before a commit is created?
- post-merge
- pre-push
- pre-commit Correct answer
- post-commit
Explanation: <code>pre-commit</code> runs before the commit operation is completed.
2 Make the Hook File Executable
chmod +x .git/hooks/pre-commit____
chmod +x .git/hooks/pre-commit+x
Explanation: <code>chmod +x</code> adds executable permission to the file, allowing Git to run the hook script.
3 Hook Concepts
| Term | Meaning |
|---|---|
pre-commit |
Runs before a commit |
post-commit |
Runs after a commit |
.git/hooks |
Directory containing hook files |
chmod +x |
Grants executable permission |