Skip to content

koppor/magic-merge-commit

Repository files navigation

Create Magic Merge Commit

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>

Installation

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>

Step-by-step description of the scenario

1. First pull request is created

The pull request starts with one commit.

First PR

2. Second pull request is created

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.

Second Commit

3. First pull request is merged in to main

First PR Merged

4. main branch has its own commit

The conflicting change in main.

Main has first commit

5. Conflict Arises with the main branch

The second commit in the pull request conflicts with the main branch, which has its own commit.

Conapsflict with main

6. Conflicting state visualized with gitk

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.

Conflicting state

7. The magic commit is created

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.

Magic Commit

8. Merge without conflicts

This "magic" merge commit now merges cleanly.

No Conflicts

More information

  • The tool gh-cherry-pick is a GitHub CLI extension allowing to gh 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.

About

Creates a magic merge commit to solve GitHub squash-merge issues

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages