Git
File Deletion
Version Control
Troubleshooting
Source Code Management

Find when a file was deleted in Git

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Git tracks every change to your repository, including file deletions. If a file was deleted in a past commit, you can find exactly when it was removed, who deleted it, and even recover its contents. The key tools are git log with --diff-filter=D and --all flags.

Find When a Specific File Was Deleted

bash
git log --all -- path/to/deleted-file.txt

This shows the full commit history for that file, including the commit where it was deleted. The last commit in the output is the deletion commit.

bash
1# Example output:
2commit a1b2c3d (HEAD -> main)
3Author: Alice <[email protected]>
4Date:   Mon Jan 15 10:30:00 2024
5
6    Remove deprecated config file

Add --oneline for a compact view:

bash
1git log --all --oneline -- path/to/deleted-file.txt
2# a1b2c3d Remove deprecated config file
3# f4e5d6c Update config values
4# b7a8c9d Add initial config

Find All Deleted Files

List every file deletion across the entire repository history:

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

This shows all commits that deleted files, with delete mode lines indicating which files were removed:

bash
1commit a1b2c3d
2Author: Alice <[email protected]>
3Date:   Mon Jan 15 10:30:00 2024
4
5    Clean up deprecated files
6
7 delete mode 100644 config/old-settings.yml
8 delete mode 100644 src/deprecated-module.js

For a more concise output showing only filenames:

bash
1git log --diff-filter=D --name-only --pretty=format:"%h %s"
2# a1b2c3d Clean up deprecated files
3# config/old-settings.yml
4# src/deprecated-module.js

Find Who Deleted a File

bash
git log --diff-filter=D --pretty=format:"%h %an %ad %s" -- path/to/file
# a1b2c3d Alice Mon Jan 15 10:30:00 2024 Remove deprecated config file

The format placeholders:

  • %h — short commit hash
  • %an — author name
  • %ad — author date
  • %s — commit subject

Recover a Deleted File

Once you know the deletion commit, recover the file from the commit before it:

bash
1# Find the deletion commit
2git log --diff-filter=D --oneline -- path/to/file.txt
3# a1b2c3d Remove deprecated config file
4
5# Recover from the commit before deletion (a1b2c3d~1 = parent of a1b2c3d)
6git checkout a1b2c3d~1 -- path/to/file.txt
7
8# Or using git restore (Git 2.23+)
9git restore --source=a1b2c3d~1 -- path/to/file.txt

The ~1 suffix means "the parent commit." This retrieves the file as it existed just before deletion.

Search for a Deleted File by Name

If you do not remember the full path:

bash
1# Search for any deleted file matching a pattern
2git log --diff-filter=D --summary | grep "delete mode.*filename"
3
4# Search for deleted files containing "config" in the name
5git log --diff-filter=D --name-only --pretty=format:"" | grep config
6
7# More precise: list all deleted files ever
8git log --diff-filter=D --name-only --pretty=format:"" | sort -u

View the Contents of a Deleted File

bash
1# Show the file at a specific commit
2git show a1b2c3d~1:path/to/file.txt
3
4# Show the file at the last commit before deletion
5git log --diff-filter=D --oneline -- path/to/file.txt | head -1
6# a1b2c3d Remove deprecated config file
7
8git show a1b2c3d~1:path/to/file.txt

View the Deletion Diff

See exactly what was in the file when it was deleted:

bash
git show a1b2c3d -- path/to/file.txt

This shows the diff for that commit, including all the removed lines (prefixed with -).

Using git bisect to Find When a File Disappeared

For complex histories, git bisect can pinpoint the exact commit:

bash
1git bisect start
2git bisect bad HEAD              # File is missing now
3git bisect good v1.0             # File existed at v1.0
4
5# At each step, check if the file exists:
6# test -f path/to/file.txt && git bisect good || git bisect bad
7
8# Automated:
9git bisect run test -f path/to/file.txt

Diff Filter Options Reference

FilterMeaning
--diff-filter=DDeleted files only
--diff-filter=AAdded files only
--diff-filter=MModified files only
--diff-filter=RRenamed files only
--diff-filter=CCopied files only

Combine filters: --diff-filter=DR shows deleted and renamed files.

Common Pitfalls

  • File moved, not deleted: If a file was renamed or moved, git log -- old/path shows it as deleted. Use git log --follow -- new/path to track a file across renames, or --diff-filter=R to find renames.
  • Shallow clones: git clone --depth=1 does not include full history. The deletion commit may not exist in a shallow clone. Use git fetch --unshallow to get the full history.
  • The --all flag: Without --all, git log only searches the current branch. The file might have been deleted on a different branch. Always use --all when searching across the entire repository.
  • Case sensitivity: On case-insensitive filesystems (macOS, Windows), path/to/File.txt and path/to/file.txt may refer to the same file. Git tracks the exact case used at commit time.
  • Recovering to working directory: git checkout <commit> -- file stages the recovered file and adds it to the working directory. You still need to commit it to make the recovery permanent.

Summary

  • Use git log --all -- path/to/file to find when a specific file was deleted
  • Use git log --diff-filter=D --summary to list all file deletions across history
  • Recover deleted files with git checkout <commit>~1 -- path/to/file
  • Use git show <commit>~1:path/to/file to view deleted file contents without recovering
  • Always include --all to search across all branches

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions