In GitHub, how can I do a two-way merge in a PR so any lines in the target branch not explicitly deleted in the source branch are not removed?
I've attached a screenshot from a GitHub PR showing the problem.
As part of the PR, I want to:
(1) Prioritise changed lines from the source branch (right) into the target branch (left);
(2) Maintain any lines in the target branch (left) which are not explicitly deleted in the source branch (right)
The problem is that GitHub just seems to remove any lines from the source branch (left) which are not included in the target branch (right), whereas I want to merge the combination together.
If this isn't possible directly in GitHub, then is there a tool which can integrate with GitHub to do this two-way merge?
Thanks.
github-pr-screenshot-merge-problem
CodePudding user response:
GitHub uses libgit2 to do merges, which works similarly, but not identically, to how Git does merges. In both cases, the merge is a three-way merge which considers both the two heads and a commit called the merge base, which is usually the most recent common ancestor. Deletions are considered a change, and are therefore included.
There is no way to make GitHub do a two-way merge. If you want that behavior, you'll have to have a custom tool (e.g., a bot) perform that merge and push the commit to the destination branch. As long as the resulting commit contains the object ID of the parent branch as one of the commits, the PR will be automatically closed.
Git does have a two-way merge in git read-tree. I'm unsure if it provides the semantics you want, but that is the only two-way merge it offers. If you read that data into the index, you can then use git write-tree and git commit-tree to synthesize a suitable merge commit.
It may be that to get the semantics you want, you need a custom merge tool. However, it's more likely that your goal can be achieved with a different workflow that works more gracefully with Git and if you provided more information about that workflow, we could suggest such a solution.
