Skip to content

Instantly share code, notes, and snippets.

@maqdev
Last active January 24, 2024 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 main
  2. Reset the main branch to the state it was in right after PR1 was merged:
    git reset --hard [PR1-SHA-COMMIT]
    
  3. Create a patch file that captures the changes made in PR2 relative to the state of the main branch:
    git diff main..PR2 > /tmp/diff.patch
    
  4. Create a new branch resolve-merge-issues:
    git switch -c resolve-merge-issues
    
  5. Apply the changes from PR2 to the resolve-merge-issues branch:
    git apply /tmp/diff.patch
    
  6. Create a single commit that combines all the changes from PR2:
git commit -a -m "Merge commit"
  1. git switch PR2
  2. Merge the single commit created in step 6, which contains the changes from PR2, into the PR2 branch:
    git merge resolve-merge-issues
    

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