Sub topic 14
Delete, Rename and Binary File Conflicts
This chapter assumes you can already read conflict markers, run git status, and finish a merge with git add and git commit. Some conflicts have no markers to edit. Git stops because it cannot decide whether a file should exist, where it lives, or how to combine its bytes.
Modify/delete conflicts
A modify/delete conflict happens when one branch deletes a file and the other changes it. This script creates one:
git init -b main demo2 && cd demo2
printf 'a\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch -c cleanup
git rm notes.txt
git commit -m "Remove notes"
git switch main
printf 'a\nb\n' > notes.txt
git commit -am "Extend notes"
git merge cleanupGit reports CONFLICT (modify/delete) and leaves the modified file in your working tree. git status labels the path by who deleted it:
deleted by them: the branch you are merging removed it.deleted by us: your current branch removed it.
You make the decision yourself. Keep the file with git add notes.txt. Remove it with git rm notes.txt. Then run git commit.
Rename conflicts
Git does not record renames. It detects them when it merges, by comparing file contents for similarity. This has two consequences:
- If one branch renames a file and the other edits it, Git usually applies the edit to the new name, with no conflict.
- If a file is rewritten heavily, Git may see a delete plus an add instead of a rename, and the edits will not follow it.
A rename/rename conflict happens when both branches rename the same file to different names. Git reports CONFLICT (rename/rename) and leaves both names in the tree. Choose one name, then use git rm on the other and git add on the one you keep.
Binary file conflicts
Git merges text line by line. A file containing NUL bytes, such as an image or a compiled file, is treated as binary and cannot be combined. If both branches changed it, Git warns Cannot merge binary files and leaves your branch's version in place. Pick a version:
git checkout --ours -- logo.png
git add logo.png
# or take the other branch's version
git checkout --theirs -- logo.png
git add logo.pngIf neither version is right, regenerate or edit the file with the proper tool, then git add it.
Choosing a response
Before you commit, run git status and confirm that no paths remain under Unmerged paths.
Exercise
You merge a branch and git status shows deleted by us: report.txt. You want to keep the other branch's version of the file. Which command do you run, and why?
Show answer
Run git add report.txt. Your branch deleted the file and the merged branch changed it, so Git left the merged branch's version in the working tree. Staging it records the decision to keep the file, and git commit then completes the merge. git rm report.txt would delete it instead.