Here's my situation: git merge master
results in 50 files with merge conflicts. I want 45 of them to just be accepted from master
and be done, and I want to manually resolve conflicts in the remaining 5. All 45 of those files are in directory some/dir
. The other 5 are scattered elsewhere. IF I wanted just to accept master's changes for all 50 conflicts, I'd run this:
git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master
But I don't want that, like I said. I'd like something more like this:
git merge -X theirs master some/dir
so that it automatically chooses "theirs" (master's) side for all conflicts in some/dir
, but otherwise let's me manually fix the conflicts. This, however, doesn't exist.
So, my work-around is this:
SHORT DESCRIPTION:
Start a merge, manually fix just the few files I need to, among the many many conflicting files. Back up any files I just fixed. Abort the merge. Redo the merge with git merge -X theirs master
to automatically accept master
's changes for ALL conflicts in ALL files. Manually copy my few manually-fixed files back into the repo, and commit them.
This avoids having to manually fix 50 files, which can be very tedious and time-consuming when only 5 really require my attention.
FULL, DETAILED STEPS:
Start a normal merge:
git merge master
Manually resolve conflicts in just the 5 files I want to do that in, and save each file. Then, MAKE COPIES OF THEM to a location outside of the repo (or at least in a folder that is ignored by the repo). I now have copies of those 5 files I manually resolved. Here is one way to accomplish this:
mkdir -p temp # create `temp` dir inside the repo. # Note: if you're not aware, the ".git/info/exclude" file in your repo is # an **untracked** form of a ".gitignore" file for the repo. We will use # this file below. # Add a `/temp/` entry to the ".git/info/exclude" file to keep # the repo from tracking the /temp/ dir, but withOUT using the # shared ".gitignore" file since this is my own personal setting # not everyone else on the team necessarily wants. echo -e "\n# don't track this temporary folder for my arbitrary use\n/temp/" \ >> .git/info/exclude # Now manually make copies of your 5 files you just resolved conflicts in: cp some/dir/file1.cpp temp cp some/dir/file2.cpp temp cp some/dir/file3.cpp temp cp some/dir/file4.cpp temp cp some/dir/file5.cpp temp
Do
git merge --abort
to abort the merge, then dogit merge -X theirs master
to redo the merge, except this time automatically accepting all of master's changes for all 50 files (ie: for all conflicts).Now, manually copy my manually-resolved backup copies from above back into the repo on top of their respective files:
cp temp/file1.cpp some/dir cp temp/file2.cpp some/dir cp temp/file3.cpp some/dir cp temp/file4.cpp some/dir cp temp/file5.cpp some/dir
Finally, do
git add -A
andgit commit --amend
to amend them to the merge commit, and voila! I have the equivalent of an automatic resolution on the 45 files, and the manual resolution on the 5 files.
Is there a better way?
I think this way works pretty well and is pretty effective, but I'm open to learning alternatives, especially if they are faster or easier.
Update: here is the answer. The currently-most-upvoted answer is in fact not correct, and produces the wrong behavior under certain really important circumstances, despite being the most-upvoted.