CodeBlanks Logo Icon CodeBlanks

Hook

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-commit

The 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

terminal
# .git/hooks/pre-commit dosyasının içeriği

Sample solution

terminal
#!/bin/bash
echo "Checking commit"
exit 0

Expected 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

terminal
# Hook dosyasına çalıştırma izni ver
# Dosya izinlerini görüntüle

Sample solution

terminal
chmod +x .git/hooks/pre-commit
ls -l .git/hooks/pre-commit

Expected 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

Question
chmod +x .git/hooks/pre-commit____
Answer
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

All lessons in Advanced