18

I run the following command:

git cherry-pick SHA --strategy-option theirs

and get a conflict like this waiting for manual resolution:

deleted by us: SOME_FILE

Is there a way to make git automatically resolve such conflicts by adding files deleted by us?

Legat
  • 1,379
  • 3
  • 11
  • 20
  • 2
    It's not *automatic* but you can simply `git add` those files back since Git leaves the "theirs" version in the work-tree. You can do this with a script that uses `git 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". – torek Apr 11 '17 at 00:50

3 Answers3

22

deleted by us are the new files which you added in your commit (getting cherry picked). But these files are not present in the current branch (the one in which you are cherry-picking into).

So in this case you have to add these files manually by running:

git add <file-path>

However if you think, these files will no longer be needed in the current branch after cherry picking, in that case you can do:

git rm <file-path>
Rahul R.
  • 5,965
  • 3
  • 27
  • 38
12

If you are sure you want to add all "deleted by us" files, you could do:

git status | sed -n 's/deleted by us://p' | xargs git add

or, if you want to remove all "deleted by us" files:

git status | sed -n 's/deleted by us://p' | xargs git rm
Simon Edlund
  • 605
  • 6
  • 5
0

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.