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.
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:
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:
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:
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-commitruns after a successful local commit and cannot block it.' - Put the script in
.git/hooks/post-commitand make it executable. - Use it for lightweight follow-up tasks, not commit validation.
- Share hooks across a team with
core.hooksPathand a versioned hooks directory. - Keep hooks simple, fast, and free of recursive Git side effects.
Related reading
- How to configure Mac OS X term so that git has color [closed]
- How to connect existing Android Studio project to existing Github repository
- How to contribute on github anonymously via Tor?
- How to convert a Git shallow clone to a full clone?
- How to convert a normal Git repository to a bare one?
- How to convert existing non-empty directory into a Git working directory and push files to a remote repository
- How to convert existing non-empty directory into a Git working directory and push files to a remote repository
- How to copy commits from one branch to another?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.