How to Find And Restore a Deleted File in a Git Repository?
Last Updated :
28 May, 2024
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.
Similar Reads
How to Delete a File From a Git Repository?
Managing files within a Git repository often involves adding new files, modifying existing ones, and sometimes, deleting files that are no longer needed. Deleting a file from a Git repository is a simple process, but it's essential to follow the correct steps to ensure that the repository remains co
3 min read
How to Fully Delete a Git Repository Created With Init?
Creating a Git repository with git init is a great way to start managing your project with version control. Git helps developers keep track of changes in their code, collaborate with others, and maintain different versions of their projects. However, there are situations where you might want to comp
3 min read
How to Find a Deleted File in the Project Commit History?
In Software development, it's not uncommon to accidentally delete a file or realize later that a deleted file is still needed. Fortunately, Git's powerful version control system makes it possible to locate and recover deleted files from the projectâs commit history. This article will guide you throu
4 min read
How to Remove Deleted Files from a Git Repo?
Managing files in a Git repository often involves adding, modifying, and deleting files. However, sometimes files are deleted from the disk but remain tracked in the repository. To keep your repository clean and in sync with your working directory, you need to remove these files from the repository
3 min read
How to Add an Empty Directory to a Git Repository?
Adding an empty directory to a Git repository can be a challenging task, as Git doesn't track empty directories by default. However, it is essential to keep track of empty directories in a repository, especially if you are working in a team or if you want to maintain the directory structure of your
3 min read
How To Clone a Git Repository Into a Specific Folder?
Cloning a Git repository is a fundamental task for developers, allowing you to create a local copy of a remote repository. While the default behavior places the cloned repository into a directory named after the repository itself, there are instances where you might want to specify a different direc
3 min read
How to Delete a Git Repository
GAs developers and software teams are collaborating on projects, the usability of version control systems like Git to track changes and manage codebases has increased a lot. Using Git, developers can create and maintain repositories, that store the complete history and versions of projects. However,
6 min read
How to Remove .DS_Store Files From Git Repositories?
If you're a macOS user, you've probably encountered .DS_Store files. The macOS Finder creates these files to store custom attributes of a folder, such as the position of icons and the view settings. While they are harmless in themselves, they can clutter your Git repository and cause unnecessary con
3 min read
How To Reset Remote Repository to a Certain Commit in Git?
Resetting a remote repository to a specific commit in Git can be an important task, especially when you need to revert changes or roll back to a stable state. This article will guide you on how To Reset Remote Repository to a Certain Commit in Git. Table of Content Approach 1: Using `git reset` and
2 min read
How to Reset a Git Branch to a Remote Repository?
Resetting a Git branch to match a remote repository is a common task, particularly when you want to discard local changes and make your branch identical to the remote counterpart. This can be useful in scenarios where your local branch has diverged from the remote, and you want to synchronize it wit
3 min read