Enforce a merge-commit policy on the server
cat > ~/recipes-origin.git/hooks/pre-receive << 'HOOK'
#!/bin/sh
while read oldrev newrev refname; do
branch=$(echo "$refname" | sed 's|refs/heads/||')
if [ "$branch" = "master" ] && [ "$oldrev" != "0000000000000000000000000000000000000000" ]; then
for commit in $(git rev-list --first-parent "$oldrev..$newrev"); do
parents=$(git rev-list --parents -n 1 "$commit" | wc -w)
if [ "$parents" -lt 3 ]; then
echo "error: direct commit $commit rejected on protected branch master"
echo "master only accepts merge commits from a reviewed pull request"
exit 1
fi
done
fi
done
exit 0
HOOK
chmod +x ~/recipes-origin.git/hooks/pre-receive
This executable hook checks the first-parent commits being added to master and rejects a commit without two parents. It demonstrates a merge-commit policy in the local bare repository. It does not verify a pull request, reviewer approval, status checks, or every rule a hosted platform can enforce. The rejection message describes the intended team workflow; the hook itself checks commit topology.
Observe a rejected direct push
! [remote rejected] master -> master (pre-receive hook declined)
A real, genuine rejection — not a simulated message, an actual server-side script refusing the push.
Merge the two reviewed branches locally
git merge --no-ff origin/feature/salad-dressings -m "Merge pull request: salad dressings"
git push
git merge --no-ff feature/dessert-menu -m "Merge pull request: dessert menu"
git push
Each --no-ff merge creates a two-parent commit accepted by this hook. The local branches stand in for reviewed PR branches; no hosted pull-request object is created. Hosted platforms may also offer squash and rebase merges, which this particular policy does not support.
Review opening a useful PR, reviewing code, and iterating on feedback to connect this local exercise to the review workflow.
A bare Git repository can run a pre-receive hook before accepting updated refs. A nonzero exit status rejects the push. This hook requires new first-parent commits on master to be merge commits. You will observe a rejected direct push and then merge two local feature branches.
The branches stand in for the reviewed PRs discussed in this block. The hook checks commit topology; it does not create hosted pull requests or verify reviewer approval.
pre-receive hook
A real, executable script inside a bare repository's hooks/ directory, run by git itself the instant a push arrives — before any ref is updated. Confirmed directly in this lab: returning a non-zero exit status genuinely rejects the entire push, in real, unmodified git.
git merge --no-ff (as a PR-merge substitute)
git merge --no-ff creates a merge commit even when a fast-forward would be possible. Its two parents satisfy this policy. Hosted PR tools can offer other merge strategies; this command does not create or approve a PR.
Takeaways
- A pre-receive hook can enforce a server-side Git policy; this example checks merge commits on master
- Returning a non-zero exit status from a pre-receive hook genuinely rejects the entire push
- git merge --no-ff creates a two-parent commit accepted by this policy; it does not create a hosted PR
- Local branches model the review workflow here; enforcing approvals requires authenticated review data