If you get a modify/delete conflict when merging two branches, it means that
- In the branch
BRANCH_DELETED
, the file was deleted.
- In the branch
BRANCH_MODIFIED
, the file was modified.
Most answers here only explain what this means TECHNICALLY and what commands to run to either keep the file or delete it, and one even explains some underlying GIT implementation details. Another one explains what a conflict is in general, which I think is too broad and not practical. This is an attempt to give a practical and complete answer.
If a file was deleted, there can be multiple reasons for it:
- The entire file and its contents are obsolete and no longer needed.
- The file was moved or renamed.
- The file was moved to a different repository.
- The file was split into multiple files (e.g., during refactoring).
- Any combination of the above.
This is IMPORTANT, because you need to find out if the contents of the file still exist somewhere in BRANCH_DELETED
and where they are now, so you can find out if the changes made in BRANCH_MODIFIED
are still relevant and, if so, where they need to go. Even if the changes cannot be literally applied to anything that still exists, you may have to make some other changes somewhere that have the same or a similar effect. This may require understanding the code you are merging and its history, and it may even require talking to other people, so you need to think outside the box of GIT commands, GIT hashes and merging (as with most conflicts). There is no GIT command that will do this for you.
To make an informed decision, you may also need SEE the modifications that were made in branch BRANCH_MODIFIED
. You can do this with one of the commands:
git diff HEAD...<BRANCH_MODIFIED> -- <FILENAME>
git difftool HEAD...<BRANCH_MODIFIED> -- <FILENAME>
(See https://stackoverflow.com/a/44956845/2279059 for details)
Then you can make the technical decision whether to delete or keep the (modified) file. You can either use the commands from https://stackoverflow.com/a/48288738/2279059 or run
git mergetool <FILENAME>
and when prompted choose to keep the modified file or delete it. Don't worry if you delete it, you can still see the modifications in the other branch as described above.
Depending on why the file was deleted and what happened to its contents, you now need to do some combination of...
- Applying (part of) the modifications to whatever other file that the modified code has moved to
- Something completely different that is equivalent(ish) to the modifications made in
BRANCH_MODIFIED
and thus resolves the conflict.
- Any of the above, but in a different repository
- Nothing (the contents of the file, and any modifications to it, are no longer needed)
Sorry if this is obvious to a lot of people, but there is widespread misunderstanding that conflicts are a technicality that can be resolved by running the correct GIT commands, and there may be a correlation between believing this and searching for the solution on StackOverflow.