Skip to content

Commit 692e1fc

Browse files
Add GitHub Actions workflow to block merge commits in PRs (#622)
#### Motivation: Currently, there is no enforcement of a linear git history in pull requests. Contributors can merge the base branch into their feature branches, creating merge commits that make the history harder to read and bisect. This change enforces a rebase-only workflow to maintain a clean, linear commit history. #### Changes: Added a new GitHub Actions workflow (`.github/workflows/block-merge-commits.yml`) that: - Triggers on pull request events (`opened`, `synchronize`, `reopened`) - Checks out the full git history of the PR - Scans all commits between the base branch and PR head for merge commits (commits with more than one parent) - Fails the check with a clear error message if any merge commits are detected, prompting contributors to rebase their branch - Passes successfully when the PR maintains a linear history Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent bf01cde commit 692e1fc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Block Merge Commits
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
check-merges:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v6
13+
with:
14+
fetch-depth: 0 # Fetch all history to inspect commits
15+
16+
- name: Check for merge commits in PR
17+
run: |
18+
# Get the list of parents for each commit in the PR
19+
# A merge commit has more than 1 parent
20+
21+
# We only look at commits strictly between the base branch and the PR head
22+
MERGE_COMMITS=$(git rev-list --merges origin/${{ github.base_ref }}..${{ github.event.pull_request.head.sha }})
23+
24+
if [ -n "$MERGE_COMMITS" ]; then
25+
echo "::error::Merge commits detected! Please rebase your branch."
26+
echo "The following commits are merges:"
27+
echo "$MERGE_COMMITS"
28+
exit 1
29+
else
30+
echo "No merge commits found. History is linear."
31+
fi

0 commit comments

Comments
 (0)