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?

1
  • 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, 2017 at 0:50

3 Answers 3

23

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>
1
  • 1
    Or use git add . to simply add all files from the cherry-pick (ignore our deletion in our branch) Oct 28, 2022 at 0:16
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
2
  • Windows command please? Oct 28, 2022 at 0:12
  • To make this work with filename which include spaces : git status | sed -n 's/\tdeleted by us: //p' | awk '{ printf "\"%s\"\n", $0 }' | xargs git add
    – altius_rup
    Jul 4 at 16:59
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.