Skip to content

Instantly share code, notes, and snippets.

@jaraco
Created June 28, 2024 17:02
Show Gist options
  • Save jaraco/1970a84b3608c380d18665302c663ef4 to your computer and use it in GitHub Desktop.
Save jaraco/1970a84b3608c380d18665302c663ef4 to your computer and use it in GitHub Desktop.
When rebasing a merge in git, is there a way to re-use an existing conflict resolution?
Show drafts
Yes, there are a few ways to reuse existing conflict resolutions when rebasing a merge in Git:
1. Rerere (Reuse Recorded Resolution):
Git has a built-in mechanism called "rerere" (reuse recorded resolution) that can automatically reuse previously resolved conflicts.
Enable it with git config --global rerere.enabled true.
When you encounter a conflict that you've already resolved before, Git will attempt to apply the same resolution.
2. Cherry-Picking:
If you know the specific commit where the conflict was originally resolved, you can cherry-pick it onto your rebase branch.
This will apply the changes from that commit, including any conflict resolutions.
3. Manual Copying:
If you have access to the original repository where the conflict was resolved, you can manually copy the resolved files into your working directory.
Then, add the files with git add and continue the rebase.
4. Interactive Rebase (--rebase-merges):
Using git rebase -i --rebase-merges, you can interactively choose how to handle each merge commit during the rebase.
When you encounter a merge commit with a conflict, you can:
Choose to resolve it manually as usual.
Use the existing resolution from the original merge (if available).
Skip the merge commit entirely.
Important Considerations:
Merges with Modifications: If the merge commit itself has been modified (e.g., amended commit message, added commits on top of it), reusing the conflict resolution might not be possible or straightforward.
Complex Conflicts: For complex conflicts with multiple overlapping changes, reusing a previous resolution might not always be the best solution. Careful manual resolution might be required.
Example using --rebase-merges:
Start the interactive rebase: git rebase -i --rebase-merges <upstream branch>
In the todo list, find the merge commit with the conflict.
Change the command from pick to edit.
When the rebase stops at that commit, Git will show the conflict.
Instead of manually resolving it, run git checkout --ours <conflicted file> (or --theirs if you prefer the other side's changes).
Add the resolved file: git add <conflicted file>
Continue the rebase: git rebase --continue
Remember to use these techniques cautiously and always double-check the results to ensure the conflicts are resolved correctly.
Let me know if you have any other questions or need more specific guidance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment