9

Is there a difference between pre-push and pre-receive hook in git, in terms of use case or working logic?
The only difference I could understand from their documentation was in terms of the input they receive -

1.Pre-Push : Information about what is to be pushed is provided on the hook's standard input with lines of the form - local ref SP local sha1 SP remote ref SP remote sha1 LF

2.Pre-receive : For each ref to be updated it receives on standard input a line of the format -
old-value SP new-value SP ref-name LF
However, I would like to know if there are particular use cases for each hook or can they be used interchangeably?

phoenix
  • 795
  • 1
  • 7
  • 14

1 Answers1

10

One (pre-push) is a client-side hook, the other (pre-receive) is a server-side hook.

In that aspect, they are very different, and if you want to enforce consistently a given policy, you often do it in a pre-receive (server side) hook. That way, you don't have to worry about deploying a pre-push hook on each client.

Remember: hooks are local to a repo, which means a pre-push hook cannot easily be distributed to any downstream repo. But if those downstream repos are all referring to the same upstream repo, a pre-receive hook can apply to them all.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Okay. Then pre-push is a client side hook and pre-receive a server side hook, I believe? – phoenix Aug 22 '14 at 05:42
  • @user1783805 I have added the notion of downstream repo: it is important. – VonC Aug 22 '14 at 05:45
  • Thanks a lot. Clear and concise answer!! – phoenix Aug 22 '14 at 05:51
  • @user1783805 I have also addressed your question on a similar topic "[Create global pre-commit hooks for entire team](http://stackoverflow.com/q/25062860/6309)" – VonC Aug 22 '14 at 06:08
  • Should i use `pre-push` or `pre-receive` for some linting tests (like flake8) where i don't want to use `pre-commit` hooks? I want whole team to enforce the tests, a bit confused about it. Thank you. – Abhijeet Khangarot Jun 14 '18 at 04:25
  • 1
    " I want whole team to enforce the tests": then it is a pre-receive (server-side) hook, which will reject the push if the linter fails. – VonC Jun 14 '18 at 04:41