77 types :
88 - labeled
99
10- # Grant permission to write to the repository
1110permissions :
1211 contents : write
1312 pull-requests : read
1413
1514jobs :
1615 squash :
17- # Only run if the 'squash-and-merge' label was added
1816 if : github.event.label.name == 'squash-and-merge'
1917 runs-on : ubuntu-latest
2018
2119 steps :
2220 - name : Checkout PR Branch
2321 uses : actions/checkout@v4
2422 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
23+ # CORRECT: Check out the branch name, not the SHA
24+ ref : ${{ github.event.pull_request.head.ref }}
2825 fetch-depth : 0
2926
3027 - name : Configure Git
@@ -34,21 +31,16 @@ jobs:
3431
3532 - name : Squash Commits
3633 run : |
37- # The PR's target branch (e.g., 'main' or 'develop')
34+ set -e # Exit on error
3835 BASE_BRANCH=${{ github.event.pull_request.base.ref }}
39-
40- # Get the commit hash of the merge base
4136 MERGE_BASE=$(git merge-base origin/$BASE_BRANCH HEAD)
42-
4337 echo "Squashing commits down to the merge base: $MERGE_BASE"
44-
45- # Perform the soft reset to un-commit changes
4638 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 }}"
39+ # Use the PR title and body for the commit message
40+ git commit -m "${{ github.event.pull_request.title }}" -m "${{ github.event.pull_request.body }}"
5041
5142 - name : Force-Push to PR Branch
5243 run : |
53- # Push the new squashed commit, overwriting the previous history
54- git push --force
44+ # CORRECT: Explicitly specify the branch to push to
45+ BRANCH_NAME=${{ github.event.pull_request.head.ref }}
46+ git push --force origin HEAD:$BRANCH_NAME
0 commit comments