Git Reference ยท Hooks
The pre-push hook
Reference for the Git pre-push hook: arguments, the lines it reads on stdin, how to block a push, how deletions look, and how to bypass it with --no-verify.
pre-push runs on your machine during git push, after Git has talked to the remote and learned what it already has, but before any objects are sent. A non-zero exit status cancels the push. Teams use it to run a fast test suite, refuse pushes of commits marked WIP or fixup!, or block direct pushes to a protected branch name.
Unlike pre-commit, the hook is told which refs are about to be updated. It gets two command-line arguments and one line of standard input for every ref being pushed.
.git/hooks/pre-push <remote-name> <remote-url> # stdin, one line per ref: # <local-ref> <local-object-name> <remote-ref> <remote-object-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
| Name | Description |
|---|---|
$1 |
The name of the remote (origin), or its URL if you pushed to a URL directly. |
$2 |
The URL of the remote. |
| stdin | One line per ref to be updated: <local ref> <local object name> <remote ref> <remote object name>, separated by spaces. |
Exit status: 0 lets the push continue. Any non-zero value aborts it, and nothing is sent.
On the stdin lines:
- When the remote ref does not exist yet, the remote object name is the all-zero ID (40 zeros in a SHA-1 repository, 64 in a SHA-256 one).
- When you delete a remote ref (
git push origin :branchor--delete), the local ref is shown as(delete)and the local object name is all zeros. - With
--tags,--allor--mirror, there is one line per ref.
Usage notes
- Read stdin even if you do not need it, or the process that writes to you may receive a broken pipe. A
while readloop is the usual pattern. - To limit checks to commits the remote does not have yet, use the range
remote_oid..local_oid, or when the branch is new,local_oid --not --remotes. Checking the whole history on every push is slow and flags old commits that are not your concern. git push --no-verifyskips the hook. Treat it as a guard against mistakes, not as policy. Real enforcement belongs on the server in pre-receive or update.- Do not make the hook slow. Running a full test suite on every push trains people to use
--no-verify. Run quick checks, and leave the long ones to CI. - The hook runs before the pack is built, so a failing hook costs only a few milliseconds of network time.
- Git ships a
pre-push.samplein new repositories that rejects commits whose subject starts withWIP; copy it as a starting point. - The remote name may be a URL when you push to a URL, so do not assume
$1is a short name.
Related
- pre-commit, pre-receive, update
- git push
- Tutorials: Git hooks, Pushing and pulling