Git Reference ยท Hooks
The post-receive hook
Reference for the Git post-receive hook: runs once after a push on the server, reads old/new/ref lines on stdin, cannot reject, used for deploys and notifications.
post-receive runs on the server once, after every ref in a push has been updated. It reads the same input as pre-receive: one line per updated ref with the old and new object names. Because everything is already done, the hook cannot reject the push, and its exit status is ignored.
That makes it the standard place for side effects: deploying a site, triggering a CI build, sending a notification, updating a mirror, or refreshing a cache. Anything the hook prints is shown to the person who pushed, with a remote: prefix, so it also works as a friendly status message.
# $GIT_DIR/hooks/post-receive (no arguments) # stdin, one line per ref that was updated: # <old-object-name> <new-object-name> <ref-name>
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
The hook is an executable file named post-receive in the receiving repository's hooks directory. It takes no arguments.
| Input | Description |
|---|---|
| stdin | One line per ref that was updated: <old-object-name> <new-object-name> <ref-name>. Created refs have an all-zero old name, deleted refs an all-zero new name. |
| Environment | GIT_DIR is set (usually .). Push options arrive as GIT_PUSH_OPTION_COUNT and GIT_PUSH_OPTION_<n> when the server allows them. |
Exit status: ignored. A failing hook cannot undo the push, but its output (and the failure) can still appear on the client.
Usage notes
- Run only the work you need. For a deploy, check that
$refis the branch you deploy, as the second example does. A push of an unrelated branch should not touch production. - The push does not finish until the hook ends, because the client waits for the server to close the connection. Start slow jobs in the background (
nohup ... &with output redirected) or hand them to a queue. - The repository is bare on most servers, so
git checkoutneeds--work-tree=<dir>, or setGIT_WORK_TREE. Use-fso that stale files do not block the checkout. GIT_DIRis set, sogitcommands in the hook target the receiving repository. If youcdelsewhere and run othergitcommands, unset it first or you will operate on the wrong repository.- Unlike
pre-receive, all objects are already in the repository, with no quarantine, so you can safely use any reachable commit. - Sending e-mail for every push is the job of the example
contrib/hooks/post-receive-emailscript that ships with Git sources; hosting platforms implement the same idea as webhooks. - If you only want a working tree on the server to follow pushes to the checked-out branch, the
receive.denyCurrentBranch=updateInsteadsetting does that without any hook. - The hook sees refs in the order they were processed, not necessarily the order you listed them.