git recover deleted file where no commit was made after the delete
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
If you accidentally delete a file in your Git repository and realize that you haven’t committed the changes yet, don’t panic! There are effective ways to recover your file using Git. This particularly applies when changes have not been staged or committed, thus utilizing the features of Git alongside the file system capabilities.
Understanding File Deletion in Git
When you remove a file from your working directory in a Git repository, the deletion is not immediately permanent. There are two scenarios to consider:
- File not staged: The file deletion has not been staged using
git add. - File staged but not committed: The file deletion has been staged but not yet committed.
In both cases, recovery options are available because Git has a record of the file(s) from the last commit or from the staged area.
How to Recover a Non-Staged Deleted File
Using git checkout
If the deletion has not been staged, recovering the file is straightforward using the git checkout command. This command will restore the working directory file from the version in the index or in the most recent commit. Here's how you can do it:
Replace <file_path> with the relative path to your deleted file. This command tells Git to restore the file from the HEAD (the last commit), effectively undoing the deletion.
Using git restore (Recommended for Git versions 2.23 and above)
Git version 2.23 introduced the git restore command, which is more versatile and intuitive for managing changes:
This command restores the file to both your staging area (--staged) and working directory (--worktree), where --source=HEAD specifies that the file should be restored from the last commit.
How to Recover a Staged but Not Committed Deleted File
If the file deletion has been staged, you can still recover it because it's recorded in the index (staging area).
Using git restore
Follow the steps for using git restore as mentioned above. Since the deletion is staged, this command will suffice to recover your file fully:
Summary Table of Commands
Below is a summary of key Git commands that can be used to recover deleted files based on their stage:
| Status of File Deletion | Command | Explanation |
| Not staged | git checkout -- <file_path> | Restores the file from the last commit to the working tree. |
| Not staged (Git 2.23 or later) | git restore <file_path> | Restores the file from HEAD to both staging and working tree. |
| Staged but not committed | git restore --staged --worktree <file_path> | Restores the deleted, staged file back into working directory and staging area. |
Additional Tips for Recovery
- Check
git statusoften: This command helps you keep track of what has been modified, staged, or deleted. - Frequent commits: Regular commits help ensure that your changes are saved incrementally, which minimizes the loss in cases of accidental deletes.
- Backup regularly: Although Git is excellent for version control, having a backup on a different system can be a lifesaver.
Conclusion
Accidentally deleting a file in a Git repository can be troublesome, but with the right commands, recovery is straightforward. By understanding how Git handles staged and unstaged changes, you can recover deleted files effectively using either git checkout or the newer git restore. Regular practice of proactive version control habits, such as frequent commits and status checks, will further protect our project from potential disruptions caused by accidental deletions.

