Bulk resolve merge conflicts
You have a branch that conflicts with
masterin small but numerous occasions. Manually fixing each would be repetitive. Even running
git checkout --theirs -- [filename]on each would be repetitive.
We need several things here:
- 1.A way to get a list of all conflicted files
- 2.A way to interactively filter out the files we don't want (it's unlikely you know exactly which files have trivial diffs beforehand)
xargsto pass these into
A naive approach would be to use the output of
git diff. We use the
catthe output for use in pipes.
git --no-pager diff --name-only | fzf | xargs git checkout --theirs --
However, this does not distinguish between regular diffs and conflicts.
Looking at the docs, it seems like adding the
git diffdoes what we want. However,
--checkalso flags whitespace markers for some reason, which can add noise, and it adds text after the filename, which must be split out (and I'm bad at
--diff-filteroption is more promising. With
--diff-filter=U, we only list unmerged files, and when rebasing, only conflicts are shown.
So, our final solution is:
git --no-pager diff --name-only --diff-filter=U | fzf | xargs git checkout --theirs --
There's an interesting file picker tool from Facebook called PathPicker. Rather than operating on lines
fppparses input and extracts path-like entities. This doesn't filter out non-conflicted diffs, but at least the filenames will be visually organized and sectioned like they are from
git status. Thus, our command becomes:
git status | fpp
This approach relies more heavily on a UI, but is simpler.
When you are sure that all conflicts are trivial, there is a blunter, simpler solution:
git rebase --abort
git rebase -Xtheirs origin/master
While figuring this out, I also stumbled across
git checkout --conflict, which resets merge conflict markers in case you made a mistake and want to start the resolution from scratch.