Introduction

Git merges are designed to intelligently integrate changes from multiple branches. However, unexpected file deletions can occur due to conflicts, rebase errors, incorrect merge strategies, or misaligned histories. These issues often manifest when merging long-lived feature branches, rebasing old commits, or resolving conflicts in large repositories. This article explores the root causes of unexpected file deletions in Git merges and provides best practices for preventing data loss.

Common Causes of Unexpected File Deletions

1. Accidental Deletions During Merge Conflict Resolution

When a merge conflict occurs, developers may mistakenly resolve conflicts by keeping the wrong version of a file.

Problematic Merge Conflict

CONFLICT (modify/delete): config.json deleted in feature-branch and modified in main.

Solution: Ensure Conflicted Files Are Manually Restored

git checkout --ours config.json  # Keep the main branch version
git checkout --theirs config.json  # Keep the feature branch version

2. Merging a Branch That Contains an Unintentional File Deletion

If a branch mistakenly deletes files that should remain, merging it into another branch will propagate those deletions.

Detect Unintentional Deletions

git log --diff-filter=D --summary

Solution: Revert Deletions Before Merging

git checkout feature-branch
git revert COMMIT_HASH  # Restore deleted files

3. Rebasing a Feature Branch That Deleted Files

Rebasing changes the history of a branch, which can cause deleted files to be lost when replaying commits.

Problematic Rebase

git rebase main

Solution: Use Interactive Rebase to Restore Files

git rebase -i HEAD~5

Then mark the commit with `edit` and manually restore files.

4. Squashing Commits That Include File Deletions

Squashing commits can lead to files being dropped if the commit history is not correctly reviewed.

Solution: Review Changes Before Squashing

git rebase -i HEAD~3  # Carefully select commits to keep

5. Incorrect Merge Strategy Selection

Different merge strategies affect how conflicts and file deletions are handled.

Problematic Strategy

git merge -s ours feature-branch  # Ignores all changes from the feature branch

Solution: Use the Right Merge Strategy

git merge --no-ff feature-branch  # Preserves commits

Debugging and Restoring Lost Files

1. Find When a File Was Deleted

git log --diff-filter=D -- path/to/missing-file

2. Restore a Deleted File

git checkout HEAD~1 -- path/to/missing-file

3. Identify Changes Between Branches

git diff --name-status main..feature-branch

Preventative Measures

1. Enable Pre-Merge Review

git diff --name-status --cached

2. Use Protected Branches

git branch --set-upstream-to=origin/main

3. Automate Merge Conflict Detection

git merge --no-commit --no-ff feature-branch

Conclusion

Unexpected file deletions during Git merges can lead to lost data and broken builds. By understanding common causes such as merge conflicts, rebasing errors, and incorrect merge strategies, developers can prevent data loss. Using Git logs, reverts, and interactive rebases can help recover deleted files and maintain repository integrity.

Frequently Asked Questions

1. Why does Git delete files when merging?

Git may remove files if a branch deletes them and the deletion is not properly resolved during a merge.

2. How can I restore a file deleted by a merge?

Use `git checkout HEAD~1 -- file` to restore a deleted file from the previous commit.

3. Does rebasing delete files permanently?

Rebasing rewrites history, so files deleted in earlier commits may be lost unless manually restored.

4. What is the safest way to merge branches in Git?

Use `git merge --no-ff` to retain commit history and avoid unexpected deletions.

5. How can I prevent accidental file deletions?

Use `git log --diff-filter=D` before merging to identify deleted files.