Keywords: Git | deleted files | git log
Abstract: This article provides a detailed guide on how to list all deleted files in a Git repository, focusing on core techniques using the git log command. It explains the basic command with the --diff-filter=D option to retrieve commit records of deleted files, along with examples of simplifying output using grep. Alternative methods from other answers are also covered, such as outputting only file paths, helping users choose the right approach based on their needs. The content is comprehensive and suitable for developers in version control and repository maintenance.
Introduction
In the Git version control system, tracking and managing deleted files is a crucial aspect of repository maintenance. Users often need to review the entire history to recover lost files or analyze changes, but Git does not provide a direct command to list all deletions. This article systematically explains how to achieve this efficiently, based on common Q&A data, by combining core commands and supplementary methods.
Core Method: Using git log with the --diff-filter=D Option
To list all deleted files in a Git repository, the most effective approach is to use the git log command with the --diff-filter=D option. This command filters all commits involving file deletions and provides detailed summaries via --summary. For example, run the following command:
git log --diff-filter=D --summaryThis outputs commit information for each deleted file, including commit hash, author, date, and file path. This method not only lists files but also provides context, making it easier to trace the reasons for deletions.
Simplifying Output: Combining with grep Command
If users are only interested in file paths without full commit details, they can use pipes and the grep command to simplify the output. An example command is:
git log --diff-filter=D --summary | grep deleteThis combination filters lines containing the keyword "delete," typically displaying a direct list of deleted files and reducing redundancy, which is useful for quick reference.
Supplementary Method: Outputting Only File Paths
Referencing other answers, a more concise command can be used to directly list unique paths of all deleted files. The command is:
git log --all --pretty=format: --name-only --diff-filter=D | sort -uThis command traverses all branches with --all, omits commit information with --pretty=format:, outputs only filenames with --name-only, filters deletions with --diff-filter=D, and finally deduplicates and sorts with sort -u. The result is a clean plain-text list, suitable for scripting or further analysis.
Practical Applications and Comparison
In real-world scenarios, the choice of method depends on specific needs. If users require detailed context to understand deletion history, the core method is more appropriate; for automated tasks or quick reporting, the supplementary method may be more efficient. For example, when recovering files, the core method provides commit references, while the supplementary method suits batch processing. Users are advised to apply these flexibly based on project scale and objectives.
Conclusion
Through the commands introduced in this article, users can easily list all deleted files in a Git repository. The core method, based on git log --diff-filter=D --summary, offers rich commit information; the simplified and supplementary methods optimize output formats. Mastering these techniques enhances version control efficiency and supports better repository management and recovery tasks.