2

In our project, we usually use a rebase-and-push workflow. Large features on different branches are merged through GitHub's UI, which show up as "Merge pull request".

Sometimes one of us does something wrong and ends up with a commit that looks like "Merge remote-tracking branch 'origin/master'". Is there any way that we can auto-deny a push to master if the push contains commits with "Merge remote-tracking branch" in the title?

Aaron Franke
  • 3,268
  • 4
  • 31
  • 51

3 Answers3

2

As others have mentioned, you can use a pre-receive hook in GitHub Enterprise, but not GitHub.com. That's because pre-receive hooks are arbitrary code, and it doesn't make sense for GitHub to run your arbitrary code on their servers.

It is possible for you to use branch protection and require pull requests to merge into certain branches, and then use GitHub Actions (or some other CI tool) to reject branches containing merges. That's the typical approach that most teams take to handle rejecting commits that don't meet some set of criteria.

bk2204
  • 64,793
  • 6
  • 84
  • 100
1

A pre-receive hook like:

#!/bin/sh

while read from to ref; do
    git log --format=%B -n 1 "$to" \
        | if grep -q pattern; then exit 1; fi
done

will reject any commit in which the subject matches the pattern. You can almost certainly make this do what you want (eg, use grep "Merge remote-tracking branch"). See githooks(5) for details.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

Considering GitHub actions do not support yet a pre-receive hook type of action, as mentioned here, you would need:

Note: Sept. 2020: GitHub Actions are starting to be available for GHE (GitHub Enterprise) in beta.


Note that Dec. 4th 2019, you can protect a branch by rejecting any push that contains a merge commit by enabling Require linear history.


VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Argh! Everytime I start to think github might be worth using I read something like this and truly wonder why it even exists. But I guess that's what the enterprise version is for. – William Pursell Nov 30 '19 at 13:58
  • To be fair, this is hardly a necessary feature, it's just a nice-to-have. – Aaron Franke Nov 30 '19 at 22:05