0Pricing
DevOps Bootcamp · Lesson

Understanding Git Hooks

Explore the different types of Git hooks and their execution points in the Git workflow.

Understanding Git Hooks is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Git Hooks?

Welcome to Git Hooks! Git hooks are powerful, customizable scripts that Git executes automatically before or after events like committing, pushing, or receiving pushed commits.

Think of them as automated checkpoints or actions in your Git workflow. They help enforce project standards, automate tasks, and integrate with other systems.

Where Hooks Live

Every Git repository comes with a .git/hooks directory. Inside, you'll find example hook scripts (usually ending with .sample).

To activate a hook, you simply remove the .sample extension and make the file executable. Git will then run it at the appropriate time.

  • Location: .git/hooks/
  • Files: Shell scripts (Bash, Python, Ruby, etc.)
  • Activation: Rename & make executable

Client-Side vs. Server-Side

Git hooks are categorized into two main types based on where they run:

  • Client-Side Hooks: Run on your local machine (e.g., before you commit or rebase). These are for personal workflow automation and local policy enforcement.
  • Server-Side Hooks: Run on the remote Git server (e.g., when you push code to a shared repository). These enforce project-wide policies for everyone.

Client Hook: pre-commit

The pre-commit hook is one of the most common client-side hooks. It runs *before* Git officially creates a commit.

If the pre-commit script exits with a non-zero status, Git aborts the commit. This is perfect for tasks like:

  • Linting code (checking for style errors)
  • Running unit tests
  • Checking for large files or sensitive data

Client Hook: commit-msg

The commit-msg hook runs *after* you've entered your commit message but *before* the commit is finalized.

This hook is useful for ensuring your commit messages follow specific conventions or formats. For example, it can check if:

  • The message starts with a ticket ID (e.g., [JIRA-123])
  • The message length is within limits
  • Specific keywords are present or absent

Client Hook: post-commit

The post-commit hook runs *immediately after* a commit has been successfully created.

Unlike pre-commit or commit-msg, this hook cannot change the outcome of the commit. It's often used for notification or integration purposes, such as:

  • Notifying team members
  • Updating a project management tool
  • Triggering a continuous integration (CI) build (though this is more common with server-side hooks)

Server Hook: pre-receive

Moving to server-side hooks, pre-receive is executed *before* any references (like branches or tags) are updated on the remote repository.

This hook is powerful for enforcing crucial repository policies, such as:

  • Preventing direct pushes to main branch
  • Ensuring all commits are signed
  • Validating committer identity

If this hook exits non-zero, the entire push is rejected.

Server Hook: post-receive

The post-receive hook runs *after* a push has successfully updated references on the remote repository.

Similar to post-commit, this hook cannot reject the push. It's commonly used for:

  • Triggering CI/CD pipelines
  • Updating external issue trackers
  • Sending email notifications to the team
  • Deploying code to a staging environment

Creating a Simple Hook

To create a hook, you simply create a file with the hook's name (e.g., pre-commit) in your .git/hooks/ directory. Let's make a very basic one:

#!/bin/sh

# This pre-commit hook will always succeed
echo "Running pre-commit hook..."
exit 0

Making Hooks Executable

After creating your hook script, you must make it executable. Git won't run it otherwise. Use the chmod command in your terminal:

chmod +x .git/hooks/pre-commit

Now, every time you try to commit, Git will execute this script!

Quick Check: Hook Timing

Git hooks run at specific points in your workflow. Understanding their timing is key to using them effectively.

Recap: Understanding Git Hooks

You've taken the first step into the powerful world of Git hooks!

  • Git hooks are scripts that automate actions during Git events.
  • They live in the .git/hooks/ directory of your repository.
  • Hooks are either client-side (local machine) or server-side (remote repository).
  • Key hooks like pre-commit and pre-receive can enforce policies by blocking operations.
  • Hooks like post-commit and post-receive are for post-action notifications or integrations.

Next, we'll dive into implementing practical examples of specific hooks!

Frequently asked questions

Is the “Understanding Git Hooks” lesson free?

Yes — the full text of “Understanding Git Hooks” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Understanding Git Hooks”?

Explore the different types of Git hooks and their execution points in the Git workflow. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Understanding Git Hooks” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Git Hooks
  2. Pre-commit & Post-merge Hooks
  3. Customizing Git Configuration
  4. Sharing Hooks with Husky
← Back to DevOps Bootcamp