File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # .github/workflows/squash-pr.yml
2+
3+ name : Squash Commits
4+
5+ on :
6+ pull_request :
7+ types :
8+ - labeled
9+
10+ # Grant permission to write to the repository
11+ permissions :
12+ contents : write
13+ pull-requests : read
14+
15+ jobs :
16+ squash :
17+ # Only run if the 'squash-and-merge' label was added
18+ if : github.event.label.name == 'squash-and-merge'
19+ runs-on : ubuntu-latest
20+
21+ steps :
22+ - name : Checkout PR Branch
23+ uses : actions/checkout@v4
24+ with :
25+ # Check out the head of the PR
26+ ref : ${{ github.event.pull_request.head.sha }}
27+ # Fetch all history so we can rebase
28+ fetch-depth : 0
29+
30+ - name : Configure Git
31+ run : |
32+ git config user.name "github-actions[bot]"
33+ git config user.email "github-actions[bot]@users.noreply.github.com"
34+
35+ - name : Squash Commits
36+ run : |
37+ # The PR's target branch (e.g., 'main' or 'develop')
38+ BASE_BRANCH=${{ github.event.pull_request.base.ref }}
39+
40+ # Get the commit hash of the merge base
41+ MERGE_BASE=$(git merge-base origin/$BASE_BRANCH HEAD)
42+
43+ echo "Squashing commits down to the merge base: $MERGE_BASE"
44+
45+ # Perform the soft reset to un-commit changes
46+ git reset --soft $MERGE_BASE
47+
48+ # Commit all the changes as a single commit using the PR title
49+ git commit -m "${{ github.event.pull_request.title }}"
50+
51+ - name : Force-Push to PR Branch
52+ run : |
53+ # Push the new squashed commit, overwriting the previous history
54+ git push --force
You can’t perform that action at this time.
0 commit comments