Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
.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

Bash Read the ref lines on stdin

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.

Bash Block WIP commits

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 :branch or --delete), the local ref is shown as (delete) and the local object name is all zeros.
  • With --tags, --all or --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 read loop 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-verify skips 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.sample in new repositories that rejects commits whose subject starts with WIP; copy it as a starting point.
  • The remote name may be a URL when you push to a URL, so do not assume $1 is a short name.