Git
Deleted Files
Git Repository
Version Control
File Recovery

How can I list all the deleted files in a Git repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In a Git repository, tracking the history of file modifications, including deletions, is crucial for managing a project's lifecycle. Knowing how to list all the deleted files can help in auditing, recovering necessary files, or understanding codebase evolution. This article delves into the methods to identify and list deleted files within a Git repository, detailing commands, options, and practical scenarios.

Understanding Deleted Files in Git

When a file is deleted in Git, it doesn't disappear completely. Instead, it becomes part of the commit history, reflecting the changes. Developers can inspect and restore deleted files using various Git commands. The Git history is a chronological sequence of project states, and each commit moves the project from one state to the next. These states can include added, modified, and deleted files.

Techniques to List Deleted Files

Several Git commands help in identifying deleted files. Here, we explore some of the most commonly used strategies.

Using `git log` and Grep

The `git log` command, combined with `grep`, can be employed to pinpoint deleted files. This involves searching through commit diffs to find patterns associated with deletions.

  • `--diff-filter=D`: This option limits the output to commits that have deleted files.
  • `--summary`: Adds a summary of changes made in each commit.
  • `grep delete`: Filters the output to show only lines containing the keyword "delete."
  • `--pretty=format:"%h - %an, %ad : %s"`: Formats the output to show the commit hash, author name, date, and the subject of the commit.
  • `--name-status`: Displays the status of the files within each commit (e.g., `A` for added, `D` for deleted).
  • Replace `COMMIT1` and `COMMIT2` with the respective commit hashes you want to compare.
  • `--name-only`: Limits output to only the names of the changed files.
  • Displays each deleted file with its associated commit hash and message.

Course illustration
Course illustration

All Rights Reserved.