CodeBlanks Logo Icon CodeBlanks

What is a hook?

Learn that a hook is an event-triggered script.

What Is a Git Hook?

A Git hook is a script that runs automatically when specific Git events occur. These scripts are located in the repository's .git/hooks directory and can be triggered before or after operations such as commits. This lets you automate repetitive checks instead of performing them manually.

.git/hooks/
  pre-commit
  commit-msg

The Location of Hook Files

Git hook files are located in the repository's .git/hooks directory by default. When a repository is initialized with git init, example files with the .sample extension are placed in this directory. To enable a hook, you can remove the extension from the relevant sample file and prepare an executable script.

.git/hooks/pre-commit.sample
.git/hooks/commit-msg.sample

Hooks run in the local repository; these scripts are not automatically shared when another user clones the repository.

Practice Build a Hook File Path

Assign the path of the pre-commit file inside the .git/hooks directory to the hookPath variable. Then print the path.

Starter code

terminal
// Hook dosyasının yolunu tanımla
const hookPath = "";

// Yolu yazdır

Sample solution

terminal
const hookPath = ".git/hooks/pre-commit";
console.log(hookPath);

Expected output

.git/hooks/pre-commit

Questions and answers

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

1 What is the main purpose of Git hooks?

  • Automatically deleting all repository branches
  • Encrypting commit history
  • Converting a remote repository into a local repository
  • Running scripts during specific Git events Correct answer

Explanation: Hooks are scripts associated with specific Git events and run automatically when those events occur.

2 Complete the Hook Directory

Question
.git/____/pre-commit
Answer
.git/hooks/pre-commit

Explanation: Git hook files are stored in the .git/hooks directory by default.

3 Basic Hook Concepts

Term Meaning
Hook Event-triggered script
.git/hooks Directory for hook files
Event Event that starts a script
.sample Sample hook extension

All lessons in Advanced