Accidentally deleting a file from a Git repository does not necessarily mean the file is lost. If the file existed in an earlier commit, Git can restore it from that version.
This is especially useful when you want to recover one specific deleted file without reverting the entire commit or branch.
1. Find the Commit Where the File Was Deleted
First, identify the commit where the file was deleted and the commit immediately before it.
For example:
e180dbaa52... commit-B: deleted the file
281c084b57... commit-A: file still existedIn this example, the file existed in:
281c084b57...So this is the commit we can use as the source for restoring the file.
You can inspect the commit history with:
git log --oneline --allIf you want to find commits related to a particular file, you can also use:
git log --all -- path/to/file2. Identify the Path of the Deleted File
Next, determine the exact path of the deleted file relative to the Git repository root.
For example:
Project/deletedFile.csThe path is important because git restore needs to know exactly which file should be recovered.
You can inspect the deleted files in a commit using:
git show --name-status <commit-hash>For example:
git show --name-status e180dbaa52A deleted file will typically appear with the status:
D Project/deletedFile.csHere, D means Deleted.
3. Restore the Deleted File
Once you know the commit where the file still existed and its path, use git restore:
git restore --source=281c084b57... -- Project/deletedFile.csThe general syntax is:
git restore --source=<commit-hash> -- <path-to-deleted-file>This restores the specified file from that commit into your current working directory.
It does not automatically create a new commit.
4. Restore Directly From the Deletion Commit's Parent
If you already know the commit that deleted the file, you don't necessarily need to find the previous commit manually.
Suppose:
e180dbaa52...is the commit that deleted the file.
You can use:
git restore --source=e180dbaa52^ -- Project/deletedFile.csThe ^ notation means:
Use the parent of this commit.
So:
e180dbaa52^refers to the commit immediately before e180dbaa52.
This is convenient because the parent commit represents the repository state before the deletion.
5. Check the Restored File
After running the restore command, check your working tree:
git statusYou should see the restored file as a new change:
Changes not staged for commit:
modified: Project/deletedFile.csIf the file was completely absent before restoration, it will normally appear as an untracked or modified change depending on the repository state.
You can inspect the restored content before committing:
git diff -- Project/deletedFile.cs6. Commit the Restored File
Once you have verified that the file is correct, stage it:
git add Project/deletedFile.csThen create a new commit:
git commit -m "Restore deleted file"The original deletion remains part of Git history, while the new commit restores the file.
Example
Suppose your history looks like this:
A1B2C3D Add UserService.cs
E4F5G6H Update UserService.cs
I7J8K9L Delete UserService.csThe file existed in:
E4F5G6Hand was deleted in:
I7J8K9LYou can restore it with:
git restore --source=E4F5G6H -- src/Services/UserService.csOr, using the deletion commit's parent:
git restore --source=I7J8K9L^ -- src/Services/UserService.csThen verify:
git statusand commit it if everything looks correct:
git add src/Services/UserService.cs
git commit -m "Restore UserService"Important: git restore vs. git revert
These commands solve different problems.
git restore is useful when you want to recover a specific file from another version:
git restore --source=<commit> -- <file>git revert creates a new commit that reverses the changes introduced by an existing commit:
git revert <commit>If a commit deleted several files but you only want to recover one particular file, git restore is generally the more targeted approach.
Final Takeaway
To restore a deleted file from Git:
Find the commit where the file was deleted.
Identify the file's repository-relative path.
Restore it from the previous commit:
git restore --source=<previous-commit> -- <file-path>Or directly from the deletion commit's parent:
git restore --source=<deletion-commit>^ -- <file-path>Then verify the file, stage it, and commit the restoration if required.

Join the conversation! Your thoughts help the community grow.