Find When a File Was Deleted in Git

Better Stack Team
Updated on July 25, 2024

To find when a file was deleted in Git, you can use the git log command along with options that allow you to trace the history of a specific file. Here’s how you can do it:

Using git log to Find File Deletion

  1. Use git log -- <file-path>: Replace <file-path> with the path to the file you are interested in. This command will show the commit history that affected the specified file.

     
    git log -- <file-path>
    

    For example, to find when a file named example.txt was deleted:

     
    git log -- example.txt
    
  2. Identify Deletion Commit: Look through the output of git log to find the commit where the file was deleted. The commit message or diff in that commit should indicate the deletion.

    Typically, a deletion commit looks like this:

     
    commit abcdef1234567890abcdef1234567890abcdef12
    Author: Author Name <author@example.com>
    Date:   Thu Jun 1 12:00:00 2023 +0200
    
        Deleted file: example.txt
    

    The message will vary depending on the author and time of deletion.

  3. View File History Before Deletion: To see the history of the file leading up to its deletion, including its contents and changes, you can use the p option with git log:

     
    git log -p -- <file-path>
    

    This command displays the commit log along with the diff for each commit that affected the specified file. It helps in understanding how the file evolved and what changes led to its deletion.

Additional Options

  • Find Deletion in Specific Branch or Commit Range: If you suspect the deletion occurred within a specific branch or commit range, you can specify that range with git log:

     
    git log -- <file-path>    # All history of the file
    git log --since="3 months ago" -- <file-path>   # History within a time range
    git log branch-name -- <file-path>   # History in a specific branch
    
  • Using git rev-list: Another approach is to use git rev-list with -max-parents=0 to find the first commit where the file was deleted:

     
    git rev-list -n 1 --max-parents=0 HEAD -- <file-path>
    

    This command will output the commit hash where the file was deleted.

Note:

  • Garbage Collection: If the deletion happened a long time ago and Git's garbage collection has occurred, it may be more challenging to retrieve the exact deletion commit. Ensure you have recent access to the repository's history to mitigate this issue.

By using these Git commands and options, you can effectively trace when a file was deleted in your Git repository and understand the context and history leading up to the deletion.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github