Local executing hook after a git push?

This type of hook is not supported by Git. It falls outside the valid reasons for a Git hook as given by Git's maintainer.

The introductory remark in the above linked message speaks almost directly to your case:

I do not particularly like hooks that act after an operation is initiated locally and act solely on local data. This is maybe because I still consider git tools building blocks suitable for higher level scripting more than other people do.

P.S. A “Single Push” Hint

  • There are too many caveats for a full explanation, but if you can figure it all out you should be able to deal with the details.

An extra pushurl to a local repo with an “alternates” objects store could give you a low overhead way to locally execute a push hook. But really, the effort is much more than git push upstream && update-web-server (maybe in a shell alias, git alias, or script).


Another solution to this problem is to have a wrapper for git push that executes .git/hooks/pre-push and .git/hooks/post-push scripts before and after the git push call. A possible wrapper could look like this:

#!/bin/sh

GIT_DIR_="$(git rev-parse --git-dir)"
BRANCH="$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD))"

PRE_PUSH="$GIT_DIR_/hooks/pre-push"
POST_PUSH="$GIT_DIR_/hooks/post-push"

test -x "$PRE_PUSH" &&
    exec "$PRE_PUSH" "$BRANCH" "$@"

git push "$@"

test $? -eq 0 && test -x "$POST_PUSH" &&
    exec "$POST_PUSH" "$BRANCH" "$@"

Saved as git-push-wh somewhere in your PATH, it can then be called as git push-wh if you want to push with hooks.


I recently came across the same issue. I wanted a hook so that a push from my git submodule would commit the new submodule reference in the 'superproject'.

As Chris mentioned, the best way is to just use a git alias, like this:

$ git config alias.xpush '!git push $1 $2 && update-server.sh'
# (remember the backslash before the ! if your shell requires it)

This adds the following to your .git/config file:

[alias]
  xpush = !git push $1 $2 && update-server.sh

And so now, if you type:

$ git xpush

your changes will be pushed, and then update-server.sh will be executed.

Tags:

Git

Githooks