Squash & Merge Implementation


I’m trying to manually implement Github’s Squash&Merge functionality.

After looking many forums like stackoverflow.com I’m still struggling with the issue.

The scenario is as follows:

  • master is a protected branch (configured on github)
  • a service user _svc _is permitted to push to master (configured on github)
  • other users don’t have such permissions
  • github PR status check is set to SUCCESS before execution of any set of git commands below
  1. This merges successfully without squash:
    git checkout origin/master

git merge ${PR-Branch}
git push origin HEAD:master
git push origin --delete ${PR-Branch} 

  1. This closes the PR, but there is no update seen on master:

git checkout origin/master
git merge --squash --commit ${PR-Branch}
git push origin HEAD:master
git push origin --delete ${PR-Branch}

  1. This fails to push to master with the error “GH006: Protected branch update failed”  (despite that the PR is set to SUCCESS):

git checkout origin/master  
git merge --squash ${PR-Branch}
git commit -am"comment"
git push origin HEAD:${m_mainBranch}
git push origin --delete ${m_prBranch}

Will appreciate your help on this.


Does it work if you push from a local branch named master to the remote branch master instead of using a detached HEAD like in your examples?

@lee-dohm, thanks for your response.

I’ve tried this:

git checkout master  
git merge --squash ${PR-Branch}  
git commit --file .git/SQUASH\_MSG   
git push -u origin master  
git push origin --delete ${PR-Branch}

The first push still results in:

EXITCODE 0remote: error: GH006: Protected branch update failed for refs/heads/master

Are you doing the push using the credentials for the svc user?

I’ve consulted with some others on what you’re running into. We’ve got a few questions:

  1. Is this repo that you’re experimenting with private?
  2. If so, please contact private Support at https://github.com/contact with all of this information so they can take a deeper look at things
  3. If not, please let us know what repo you’re accessing to get these errors
  4. What software, if any beyond plain old Git, are you using to perform these actions?
  5. If it is only Git, which version of Git is it?

Prior to the git commands I’ve described previously we run those:

git config user.name ${GIT_USERNAME}  
git config user.email "jenkins@jenkins.com"  
git remote set-url origin https://${GIT_USERNAME}:${ctx.GIT_PASSWORD}@${GIT_REPO}

It is the same svc user we use to checkout the repository in the Jenkins job.

  1. It is a private repo indeed, will try to contact the private support as you suggested

  2. It is running in a shell script section as the last stage of a Jenkins pipeline

  3. git version on Jenkins is 2.11.0

Actually this is solvable using github API.

curl -X PUT -H "Authorization: token ${adminToken}" --data '{"merge_method":"squash"}' https://github.<>.com/api/v3/repos/:org/:repo/pulls/${PR_ID}/merge
1 Like