Sub topic 14
Delete, Rename and Binary File Conflicts
This chapter assumes you can start a merge, read conflict markers, and finish a merge with git add and git commit. It covers the conflicts that don't fit that pattern: a file deleted on one branch and edited on the other, a file renamed in two ways, and a file Git can't merge line by line.
Why these conflicts look different
A text conflict is two edits to the same lines. Git writes markers into the file, and you edit them away. The conflicts in this chapter often have no markers to edit:
- A deleted file has no content to put markers in.
- A rename conflict is about the file's path, not its lines.
- A binary file has no meaningful lines, so Git can't merge it at all.
In each case Git leaves the path unmerged in the index. You decide the outcome and tell Git with git add or git rm. git status is your main tool, because it names the kind of conflict.
| Status label | Short code | Meaning |
|---|---|---|
| both modified | `UU` | Both sides changed the file. Markers are in the file, or the file is binary. |
| both added | `AA` | Both sides created the same path with different content. |
| deleted by us | `DU` | Your branch deleted the file and the other branch changed it. |
| deleted by them | `UD` | The other branch deleted the file and yours changed it. |
| added by us / added by them | `AU` / `UA` | Only one side has the path. This often appears in rename conflicts. |
"Us" is the branch you are on, and "them" is the branch you are merging in. During a rebase these swap, as the rebase chapter explains.
Modify/delete conflicts
One branch deleted a file while the other edited it. Git can't tell whether the edit should survive. This script builds the situation:
git init -b main demo-delete && cd demo-delete
printf 'legacy v1\n' > legacy.txt
printf 'keep\n' > keep.txt
git add . && git commit -m "Add files"
git switch -c cleanup
git rm legacy.txt
git commit -m "Remove legacy.txt"
git switch main
printf 'legacy v2\n' > legacy.txt
git commit -am "Update legacy.txt"
git merge cleanupGit prints a message like CONFLICT (modify/delete): legacy.txt deleted in cleanup and modified in HEAD. git status lists the file under deleted by them. The working tree keeps your edited version, so you can still read it.
Pick one outcome:
# Option 1: accept the deletion
git rm legacy.txt
# Option 2: keep the file, with your edits
git add legacy.txt
git commitBefore choosing, find out why the other side deleted the file. Look at the commit that removed it:
git log --oneline --diff-filter=D cleanup -- legacy.txtIf the deletion was part of a move or a replacement, your edit may belong in the new location. In that case, delete the old file and port the edit by hand.
git ls-files -u shows only stages 1 (base) and 2 (ours) for a "deleted by them" file. Stage 3 is missing because the other side has no such file. You can print the base version with git show :1:legacy.txt.
Renames
Git doesn't store renames. It stores snapshots, and during a merge it detects a rename by comparing file contents. A deleted file and an added file that are at least 50% similar are treated as a rename.
This detection lets a rename on one branch and an edit on the other combine without a conflict:
git init -b main demo-rename-edit && cd demo-rename-edit
printf 'one\ntwo\nthree\nfour\nfive\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch -c tidy
git mv notes.txt docs.txt
git commit -m "Rename notes to docs"
git switch main
printf 'one\ntwo\nthree\nfour\nfive\nsix\n' > notes.txt
git commit -am "Add a sixth line"
git merge tidyThis merge succeeds, and docs.txt ends up with six lines. The edit follows the file to its new name.
Rename/rename conflicts
A conflict appears when both branches rename the same file to different names. Git can't choose which name is right:
git init -b main demo-rename-rename && cd demo-rename-rename
printf 'one\ntwo\nthree\nfour\nfive\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch -c feature
git mv notes.txt ideas.txt
git commit -m "Rename to ideas.txt"
git switch main
git mv notes.txt todo.txt
git commit -m "Rename to todo.txt"
git merge featureThe message reads like CONFLICT (rename/rename): notes.txt renamed to todo.txt in HEAD and to ideas.txt in feature. Both new paths appear in git status, one as added by us and one as added by them. To keep the name todo.txt:
git rm ideas.txt
git add todo.txt
git commitIf both sides also edited the contents, merge the contents into the file you keep before you run git add. Check the result of each step with git status.
Rename/delete and add/add conflicts
- Rename/delete: one branch renamed a file and the other deleted it. Decide whether the file should exist at the new path or not at all. Then
git addthe new path orgit rmit. - Add/add: both branches created a new file at the same path with different content. Git treats this as a content conflict with markers (status
both added). Resolve it like any text conflict. - Directory renames: if one branch moves a directory and the other adds a file inside the old directory, newer Git versions may move the new file into the new directory. Always check where new files ended up after such a merge.
When rename detection fails
A file that was renamed and heavily rewritten may fall below the 50% similarity threshold. Git then sees one file deleted and another added, so you get a modify/delete conflict instead of a clean merge. You can lower the threshold for a single merge:
git merge -X find-renames=30% featureA lower threshold finds more renames, but it can also pair files that are unrelated. Review the result before you commit. The same option works with git rebase -X find-renames=30%.
Binary file conflicts
Git decides a file is binary when it finds a NUL byte near the start, or when .gitattributes says so. It can't merge binary files, because there are no lines to compare. If both branches changed the same binary file, the merge stops. This script uses a tiny file with NUL bytes to stand in for an image:
git init -b main demo-binary && cd demo-binary
printf '\000\001\002' > logo.bin
git add logo.bin && git commit -m "Add logo"
git switch -c rebrand
printf '\000\003\004' > logo.bin
git commit -am "New logo from the design team"
git switch main
printf '\000\005\006' > logo.bin
git commit -am "Logo with transparent background"
git merge rebrandGit prints warning: Cannot merge binary files: logo.bin (HEAD vs. rebrand) followed by a content conflict. No markers are written, because they would corrupt the file. The working tree keeps your version, which is ours.
You can't combine the two versions with a text editor. Choose one, or produce a new file with the right tool:
# take the other branch's version
git checkout --theirs -- logo.bin
git add logo.bin
git commitTo keep your own version, use git checkout --ours -- logo.bin instead. If the right answer is a mix, such as two edits to the same image, open both versions in the application that made the file. Save the combined result under the same path, then git add it. Extract each version first:
git show :2:logo.bin > logo-ours.bin
git show :3:logo.bin > logo-theirs.binDelete these helper files before you commit, or they end up in the merge commit.
Choosing a resolution
After you resolve every path, run git status and confirm nothing is listed under "Unmerged paths". Then build or test the project. A merge that removes or renames files can leave other files pointing at paths that no longer exist, and Git doesn't detect this.
Common errors
- Using
git addwhen you meant to delete. For a "deleted by them" file,git addkeeps the file. Usegit rmto accept the deletion. - Resolving only one of two rename paths. After a rename/rename conflict, both paths are unmerged until you handle each one.
- Trusting a clean merge too much. A merge can succeed while an edit lands in a file that is no longer used. Check
git diff --stat HEAD~1after the merge commit. - Running
git checkout --theirson the wrong merge direction. Check which branch you are on before you pick a side. In a rebase the meanings are reversed. - Committing helper files such as
logo-ours.bin. Rungit statusbeforegit commit.
Exercise
You merge a branch and git status lists docs/api.md as "deleted by them". The file exists in your working tree with your latest edits. A teammate tells you the other branch moved the content to docs/reference/api.md. What do you do to finish the merge, and why isn't git add docs/api.md enough?
Show answer
Compare docs/reference/api.md with your version of docs/api.md and copy your edits into the new file. Then run git rm docs/api.md to accept the deletion, git add docs/reference/api.md, and git commit. git add docs/api.md would keep the old file, so the repository would have two copies of the document and your edits would not be in the one the other branch uses. Git didn't detect the move as a rename here, probably because the content changed too much to reach the 50% similarity threshold.
Exercise
A merge stops with warning: Cannot merge binary files: banner.png (HEAD vs. redesign). You want the version from the redesign branch. Write the commands to resolve it, and say what banner.png in your working tree contains before you run them.
Show answer
Run git checkout --theirs -- banner.png, then git add banner.png, then git commit. Before that, the working tree contains your own branch's version of banner.png (ours), and Git wrote no conflict markers into it.