How to use git checkout for Recovering Deleted Files In Git
Alternatively, you can use git checkout with the — separator to restore a deleted file directly from a specific commit without first identifying the parent commit. Here’s how:
Step 1: Find the Deletion Commit Hash
Use the git log command with the file path to find the commit hash where the file was last present.
git log -- config/old_config.yml
Note the commit hash where the file was last modified before deletion.
Step 2: Restore the File
Use git checkout with the commit hash directly:
git checkout <commit-hash> -- config/old_config.yml
This command checks out the file as it was in the specified commit and places it in your working directory.
Step 3: Stage and Commit the Restored File
Add and commit the restored file as shown earlier.
git add config/old_config.yml
git commit -m "Restore deleted file config/old_config.yml"
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 through the steps to find and restore deleted files in a Git repository.