Skip to content

Instantly share code, notes, and snippets.

@maqdev
Last active October 1, 2024 20:16
Show Gist options
  • Save maqdev/c79cd0af569c392dc2ca2452af103a64 to your computer and use it in GitHub Desktop.
Save maqdev/c79cd0af569c392dc2ca2452af103a64 to your computer and use it in GitHub Desktop.
Resolving git conflicts for chained PRs after squash/merge commit

Assuming that there was a:

  • PR1 based on branch main
  • PR2 based on PR1

And after squash-merging PR1 into the main branch, sometimes we can't just merge PR2 due to a conflicts arising from the commits in PR1 being squashed.

Here's the step-by-step explanation of the Git workflow to resolve this issue:

  1. git switch PR1 # if branch was deleted - restore it
  2. Create a patch file that captures the changes made in PR2 relative to the state of the PR1, prior to merging it into main branch:
    git diff PR1..PR2 --full-index --binary > /tmp/diff.patch
    
  3. Create a new branch resolve-conflicts:
    git switch -c resolve-conflicts
    
  4. Apply the changes from PR2 to the resolve-conflicts branch:
    git apply /tmp/diff.patch
    
  5. Create a single commit that combines all the changes from PR2:
    git commit -a -m "Merge commit"
    
  6. git switch PR2
  7. Merge the single commit created in step 5, which contains the changes from PR2, into the PR2 branch:
    git merge resolve-conflicts
    

After completing these steps, the actual main branch should now be mergeable into PR2 without conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment