Have you ever created a branch based on another branch?
Then that other branch got squash-merged into main
.
Now your pull request, which was based on that branch, can’t merge cleanly because Git sees your old commit as conflicting with the squashed commit in main?
This tool generates a special merge commit that links your branch to the squashed history in main
, eliminating the conflict and letting you merge main
without trouble.
Execute the tool with following command:
jbang do@koppor/magic-merge-commit <pr-number>
To have jbang
working, you need to install it. Find information at the jbang page.
If you don't want to install jbang, place gg.cmd
into the root of your git repository and execute as follows:
- Linux/macOS:
sh ./gg.cmd jbang do@koppor/magic-merge-commit <pr-number>
- Windows:
.\gg.cmd jbang do@koppor/magic-merge-commit <pr-number>
The pull request starts with one commit.
Another improvement is needed. It should be reviewed separate pull request, but needs the first pull reuqest.
Therefore, a new branch is created and a commit is added to it. Finally, a second pull request is created, based on the first one, which is still opened.
The conflicting change in main
.
The second commit in the pull request conflicts with the main
branch, which has its own commit.
The first pull request was added as commit to the main
branch.
There is no git merge commit and there is no git connect with the pull request.
The only indicator is #1
in the commit message.
This tool creates a new commit that has both main
and pr-last-commit
as parents.
This commit "magically" resolves the conflict by wiring the two branches together.
This "magic" merge commit now merges cleanly.
- The tool
gh-cherry-pick
is a GitHub CLI extension allowing togh cherry-pick -pr <pr-number> -onto <target_banch>
allows to cherry-pick PRs into unrelated branches. gitk
was used to visualize the commits. Learn more about it at lostechies.- The scenario is available at squash-merge-demo.
- Background is available at a blog post.