Duplicating git client hooks on the server side

client side git hooks don't execute on the server - but why not?

Generally, you are pushing to a bare repo (a repo without working tree, where you cannot do any commit directly)
So server-side commits are more about enforcing policies than creating new commits.

If you really needed new content to be created on the server side (especially one you have no direct control, like GitLab.com), you would need:

  • either to activate some kind of server-side hook, which is for now only available at GitHub, with GitHub actions.
  • or setup a listener to a GitLab webhook: that webhook would call (on each push events) your listener which could in turn get the latest history, do any modification you need, create new commit and push back.

Here's a solution that works for me. It does require you to execute two short commands once within any copy of the repo.

Within any copy of your repo, do the following:

  1. Copy your .git/hooks folder and/or any hook scripts you want to use to .githooks. You can choose a different folder name than .githooks, of course. Specifically, within the top folder of your repo use

    $ cp -a .git/hooks .githooks

  2. Add .githooks to the repo

    $ git add .githooks

    $ git commit -m 'added .githooks directory'

Note you only have to do these first two steps once in any of the repos, not in every local copy.

Within each local copy of a repo you will need to

  1. Change the local repo's configuration to use .githooks instead of .git/hooks

    $ git config --local core.hooksPath .githooks

  2. At this point the local repo is poised to start making the .git/gitHeadInfo file, but won't have done so yet. Either
    1. Make a commit
    2. Execute one of the gitinfo2 hooks (which are scripts after all) manually. E.g.,

      $ .githooks/post-commit