git
post-commit hook
configuration
version control
software development

How to configure Git post commit hook

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A post-commit hook runs automatically after a commit has already been created in your local repository. It is useful for lightweight follow-up work such as notifications, local logging, or kicking off background tooling, but it is the wrong place for anything that should block a bad commit before it lands.

What post-commit Is Good For

Because post-commit runs after the commit succeeds, it cannot reject the commit or change its outcome. That makes it a good fit for tasks like:

  • printing the new commit hash
  • updating a local task tracker
  • sending a desktop notification
  • triggering a background build or local sync

If you need to prevent bad commits, use pre-commit or commit-msg instead.

Creating the Hook

Git looks for hooks in .git/hooks/ by default. Create a file called post-commit there and make it executable:

bash
1mkdir -p .git/hooks
2cat > .git/hooks/post-commit <<'HOOK'
3#!/usr/bin/env bash
4set -euo pipefail
5
6commit_hash=$(git rev-parse --short HEAD)
7branch=$(git branch --show-current)
8
9echo "[post-commit] branch=${branch} commit=${commit_hash}"
10HOOK
11
12chmod +x .git/hooks/post-commit

After that, every successful local commit runs the script.

A More Practical Example

Here is a slightly more useful hook that appends commit metadata to a local log file:

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4repo_root=$(git rev-parse --show-toplevel)
5commit_hash=$(git rev-parse HEAD)
6commit_subject=$(git log -1 --pretty=%s)
7
8printf '%s | %s | %s\n' \
9  "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
10  "${commit_hash}" \
11  "${commit_subject}" >> "${repo_root}/.git/post-commit.log"

This kind of hook is local by nature and does not interfere with the repository history itself.

Sharing Hooks With a Team

Files inside .git/hooks/ are not versioned, so teammates will not automatically receive them. To share a hook, put it in a tracked directory and point Git at that directory:

bash
mkdir -p .githooks
git config core.hooksPath .githooks

Then save the script as .githooks/post-commit and commit it to the repository. That way the hook lives in version control instead of inside each clone's private .git directory.

Shared hooks are useful, but be careful about making them too heavy. Slow hooks make developer workflows frustrating very quickly.

Environment and Execution Details

Hooks run in a shell environment that may not match your interactive terminal exactly. Do not assume aliases, shell functions, or a particular working directory layout unless you set those things yourself.

A few good habits help:

  • start scripts with a real shebang
  • use absolute or repo-root-relative paths
  • keep output concise
  • fail clearly when required tools are missing

If a hook behaves differently in an IDE than in the terminal, environment differences are often the cause.

Avoid Recursive or Heavy Work

One subtle trap is triggering another Git command from inside post-commit that creates a new commit. That can lead to recursive behavior or confusing state changes. Keep the hook focused on side effects that do not rewrite history automatically.

Likewise, avoid long-running builds, large test suites, or network operations that make every commit feel slow. A post-commit hook should be lightweight enough that developers do not start bypassing it out of frustration.

Common Pitfalls

The most common mistake is forgetting to mark the hook executable. Git will ignore a non-executable hook file.

Another issue is putting team hooks only in .git/hooks/ and assuming they are shared. They are local to one clone unless you use a tracked hooks directory and core.hooksPath.

Developers also use post-commit for validation logic even though it runs too late to stop a bad commit. Choose the hook that matches the lifecycle stage you actually need.

Summary

  • 'post-commit runs after a successful local commit and cannot block it.'
  • Put the script in .git/hooks/post-commit and make it executable.
  • Use it for lightweight follow-up tasks, not commit validation.
  • Share hooks across a team with core.hooksPath and a versioned hooks directory.
  • Keep hooks simple, fast, and free of recursive Git side effects.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.