sospedra
sospedra

Reputation: 14724

Merge a PR (not close) from git --squash

When I develop in a branch and merge it into master with:

git merge --no-ff $BRANCH
git push origin HEAD

It merges the PR in Github (the purple badge).

But if I want to merge it squashed:

git merge --squash $BRANCH
git add . && git commit -m "Merge branch '$BRANCH'"
git push origin HEAD

It remains open til I remove the branch:

git branch -D $BRANCH 
git push origin :$BRANCH

How can I merge a squashed branch and merge (not close) the PR in Github?

Here's an example repo: https://github.com/sospedra/better-pr-close

Upvotes: 1

Views: 1641

Answers (3)

Asaf
Asaf

Reputation: 1261

You can use the gh cli

gh pr merge <PR-number> -s

Upvotes: 0

Marina Liu
Marina Liu

Reputation: 38096

This is caused by the different mechanisms between git merge and git merge --squash.

Assume before merging the two branches, the commit history looks like:

…---A     master
     \
      B   alpha

Situation 1: If you are use git merge --no-ff alpha, the commit history will be:

…---A---C  master
     \ /
      B    alpha

After pushing the changes to github, PR knows commit C is the merged commit (since commit C has two parents both on master branch and alpha branch), so the PR will be closed.

Situation 2: If you are use git merge --squash alpha, the commit history will be:

…---A---C'   master
     \
      B      alpha

After pushing the changes to github, it’s hard for the PR to judge whether commit C' is the new changes for master branch or it’s the squash merged commit from alpha branch (commit C' only has one parent).

And for now, github only treat the situation1 for closing the PR since the alpha branch apparently merged into master branch. While for situation2, since the commit C' only has one parent commit A, PR won’t be closed.

Upvotes: 1

sospedra
sospedra

Reputation: 14724

In case anyone has the same problem. I contacted the GitHub team by email and they tell me is not possible right now because they use the SHA's commit to link the merge commit with the PR.

Here's the full answer:

The logic to automatically merge a Pull Request looks at the commit SHA's present on the Pull Request.

If all of the commit SHA's have been pushed to the base branch already, we'll automatically mark it as merged.

When you squash commits and push, the references to the original commits are lost.

This means that is isn't currently possible to mark a PR as merged if the commits have been squashed and pushed from the command line.

I can pass your request onto the team to consider for future improvements.

I can't promise if or when we'd add this but I'll for sure pass it along!

They'll take the request into consideration. Hopefully will be added at some point.

Upvotes: 7

Related Questions