In my case, I didn't know which file would need the conflict resolution until the error pops up. So, the scenario is : I created a new file, committed it (ID1), modified it and committed it again (ID2) in source branch (cherry-pick from), and when I used the "modified" commit ID (ID2) for cherry-pick, git complained because the file never existing in the target branch (cherry-pick into).
git cherry-pick threw the following error:
CONFLICT (modify/delete): <SOME_FILE> deleted in HEAD and modified in <SHA>... Update the CSV file content. Version <SHA>... Update the CSV file content of <SOME_FILE> left in tree.
git status showed
deleted by us SOME_FILE
Based on inspiration from answer, here's a solution I tried. The mergetool (eg: vimdiff3 used here) allows us to choose a conflict resolution: Use (m)odified or (d)eleted file, or (a)bort. Please note, merge tool creates *.orig file which needs cleaning as necessary.
sha=<enter SHA>
conflict_resolution='m'
file_path=<enter file path in the repo>
if ! git cherry-pick $sha --strategy-option theirs
then
echo "Auto Merge failed. Attempting to use merge tool."
yes $conflict_resolution | git mergetool --tool=vimdiff3 -- $file_path
git clean -f *.orig
git commit -m "From Jenkins Job: ${JOB_NAME} Build: ${BUILD_NUMBER}"
echo "Merge successful using merge tool"
else
echo "Auto Merge successful."
fi
PS: Along with cherry-pick, I guess the solution can also be applied for a typical merge scenario.
git add
those files back since Git leaves the "theirs" version in the work-tree. You can do this with a script that usesgit ls-files --stage
to find files that are present in stages 1 and 3 but absent in stage 2: since 1 = base, 2 = ours, 3 = theirs, such files are precisely those "deleted by us".