ASP.NET Tutorial: How to Get a Deleted File Back in Git After Pushing?
A file is not always lost if it is inadvertently removed from a Git repository. Git can restore the file from a previous commit if it was present.
When you wish to restore a single deleted file without undoing the entire commit or branch, this is really helpful.
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:
In this example, the file existed in:
So this is the commit we can use as the source for restoring the file.
You can inspect the commit history with:
If you want to find commits related to a particular file, you can also use:
2. Identify the Path of the Deleted File
Next, determine the exact path of the deleted file relative to the Git repository root.
For example:
The 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:
For example:
A deleted file will typically appear with the status:
Here, D means Deleted.
3. Restore the Deleted File
Once you know the commit where the file still existed and its path, use git restore:
The general syntax is:
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:
is the commit that deleted the file.
You can use:
The ^ notation means:
Use the parent of this commit.
So:
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:
You should see the restored file as a new change:
If 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:
6. Commit the Restored File
Once you have verified that the file is correct, stage it:
Then create a new commit:
The original deletion remains part of Git history, while the new commit restores the file.
Example
Suppose your history looks like this:
The file existed in:
and was deleted in:
You can restore it with:
Or, using the deletion commit’s parent:
Then verify:
and commit it if everything looks correct:
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 revert creates a new commit that reverses the changes introduced by an existing 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:
Or directly from the deletion commit’s parent:
Then verify the file, stage it, and commit the restoration if required.
ASP.NET Core 10.0 Hosting Recommendation
HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.
