Open In App

How to Find And Restore a Deleted File in a Git Repository?

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Deleting a file in a Git repository is a common task, whether intentional or accidental. Fortunately, Git's powerful version control system makes it possible to find and restore deleted files with relative ease. This article will walk you through the steps necessary to recover your lost data.

Steps to Find and Restore Deleted Files

Step 1: Identify the Commit Where the File Was Deleted

To restore a deleted file, you first need to identify the commit where the file was removed. You can do this using the git log command with some additional options to search through the commit history for deletions.

Open your terminal and navigate to your Git repository. Use the following command to search for the commit that deleted the specific file:

git log --diff-filter=D --summary | grep delete

This command does the following:

  • --diff-filter=D: Filters the commits to show only those where files were deleted.
  • --summary: Provides a summary of changes in each commit.
  • grep delete: Searches the output for lines that contain the word "delete".

The output will show lines indicating which files were deleted and the corresponding commits. For example:

commit 5f4bfc3 
delete mode 100644 path/to/deleted_file.txt

Take note of the commit hash (5f4bfc3 in this example), as you will need it in the next step.

Step 2: Find the Commit Where the File Last Existed

Once you have identified the commit where the file was deleted, you need to find the commit where the file last existed. You can use the git log command again, but this time, you'll limit the log to the specific file:

git log -- path/to/deleted_file.txt

This command will list all the commits that involved changes to path/to/deleted_file.txt. The last commit in the list before the deletion commit is the one where the file last existed in its intact form. Note this commit hash as well.

Step 3: Restore the Deleted File

Now that you have the commit hash where the file last existed, you can restore the file using the git checkout command. Use the following syntax:

git checkout <commit_hash> -- path/to/deleted_file.txt

Replace <commit_hash> with the hash of the commit where the file last existed. For example:

git checkout 3a5bfc2 -- path/to/deleted_file.txt

This command will restore deleted_file.txt to your working directory as it was in the specified commit.

Step 4: Commit the Restored File

After restoring the file, it will be present in your working directory, but it won't be committed to the repository yet. To finalize the restoration, you need to add and commit the restored file:

git add path/to/deleted_file.txt
git commit -m "Restore deleted_file.txt"

This sequence of commands stages the restored file and commits it to the repository with a message indicating that the file has been restored.

Additional Tips

  • Viewing File Content Before Restoration: If you want to see the contents of the deleted file before restoring it, you can use the git show command:
git show <commit_hash>:path/to/deleted_file.txt

This will display the content of the file as it was in the specified commit.

  • Restoring Multiple Files: If multiple files were deleted in a single commit and you want to restore all of them, you can use a similar process but iterate over each file listed in the deletion commit.

Next Article
Article Tags :

Similar Reads