CodeBlanks Logo Icon CodeBlanks

Hook scripting

Learn to write hook scripts.

What Are Hook Scripts?

Git hooks are scripts that run automatically when specific Git operations occur. Hook files are usually located in the `.git/hooks` directory and can be written as shell scripts. The script must be executable.
#!/bin/bash
echo "Hook executed"

Shell Scripts and Hook Files

A hook file can contain a script that the operating system can execute. When using Bash, the first line contains a shebang that identifies the interpreter. You can use `chmod +x` to grant execution permission.
chmod +x .git/hooks/example-hook

Practice Write a Simple Hook Script

Create a hook script that runs with Bash. Specify the Bash interpreter on the first line and print `Hook executed` to the terminal.

Starter code

terminal
echo "Mesaj"

Sample solution

terminal
#!/bin/bash
echo "Hook çalıştı"

Expected output

Hook çalıştı

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What property must a hook script have to run?

  • It must have executable permissions Correct answer
  • It must contain HTML
  • It must be uploaded to a remote repository
  • It must only be an empty file

Explanation: A hook file must have appropriate execution permissions so Git can run it.

2 Complete the Bash Hook File

Question
#!/bin/bash
_____ "Hook hazır"____
Answer
#!/bin/bash
_____ "Hook hazır"echo

Explanation: In Bash, the `echo` command prints text to the terminal.

3 Match the Hook Script Components

Term Meaning
Shebang Identifies the interpreter
Bash Is a shell environment
chmod +x Grants execution permission
.git/hooks Is the directory for hook files

All lessons in Expert