45

Does GitHub allow for pre-receive or update hooks?

What I would be looking to do is to prevent the primary branches from being pushed to (i.e. master, hotfix, develop) and require that they be merged via a GitHub pull request. This is a private repo so GitHub style forking is not an option.

Any advice on how to accomplish this would be of great help.

Anton Rudeshko
  • 1,039
  • 2
  • 11
  • 20
Jason Waldrip
  • 5,038
  • 8
  • 36
  • 59
  • 4
    Note that GH *does* allow users to fork private repo so it stays private. AFAIR, there is a limit on the number of forks though. Consult your payment plan info. – Alexander Gladysh Jun 03 '12 at 16:32

3 Answers3

19

Only people that you have listed a 'collaborators' can push to a Github hosted repository. See the repository's 'admin' page to add collaborators. Everybody else needs to submit a 'pull request' to get their additions accepted by the repository's administrator. See Github Access Permissions. (There aren't per-branch access permissions.) So, in order to accomplish your goal, you don't need hooks; what you need is already built in.

Two Notes:

  1. private Github repositories can have multiple contributors
  2. it would be unusual for Github to support pre-receive hooks (or any other server hooks) given that hooks contains arbitrary code.
CharlesB
  • 86,532
  • 28
  • 194
  • 218
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 3
    Thats great, but its a private repo that has collaborators, pre-receive hooks are a git-core feature, I was seeing if there was a way git-hub implemented them. – Jason Waldrip Jun 02 '12 at 21:30
  • See edit - server hooks run arbitrary code on the server; can't imagine Github allowing that. – GoZoner Jun 03 '12 at 15:51
  • While GitHub does not seem to allow pre-receive hooks, it does allow *post*-receive ones: https://help.github.com/articles/post-receive-hooks – Alexander Gladysh Jun 03 '12 at 16:31
  • 1
    @AlexanderGladysh The hook will simply POST a JSON to a specified URL, it still doesn't allow arbitrary code. In fact the only thing you can specify is the URL, not the hook itself. – Shagglez Jan 18 '13 at 13:59
16

Though GitHub itself doesn't allow pre-receive hooks, GitHub Enterprise version 2.6 does include pre-receive hook support. More information about it can be found here: https://help.github.com/enterprise/admin/guides/developer-workflow/using-pre-receive-hooks-to-enforce-policy/. These are instance wide pre-receive hooks to prevent any information to be pushed into any repositories.

What I would be looking to do is to prevent the primary branches from being pushed to (i.e. master, hotfix, develop) and require that they be merged via a GitHub pull request.

However, to this point, you can configure GitHub or GitHub Enterprise repositories with protected branches. What this does is prevent the branch from being deleted, force pushed, only certain people or teams can merge, or a status check is required to be passing before a merge is allowable. Please see https://help.github.com/articles/about-protected-branches/ for more information!

brntbeer
  • 353
  • 5
  • 7
7

If you're collaborating with a trusted team (which I assume you are since it's a private repo) and you want to implement this simply to keep people from accidentally violating your standard operating procedures out of pure habit, the best thing to do is distribute a custom pre-push hook script for everyone to install into their .git/hooks directory. (I did something similar recently by insisting everyone run git config branch.autosetuprebase always which implicitly changes git pull to git pull --rebase to eliminate needless merges when local unpushed commits exist)

If for some reason that doesn't work, I find that the threat of losing committer rights after the fact is usually an effective mechanism for keeping people honest.

Jay Allen
  • 465
  • 3
  • 8
  • This will not work as soon as people will merge PRs from the github UI. Which is pretty common in large teams. – JAR.JAR.beans Oct 15 '20 at 13:04
  • Sure, now it is but not when I posted the comment. Thanks for pointing it out though because that definitely reduces the utility these days. – Jay Allen Dec 04 '20 at 07:31