ASP.NET Hosting

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:

e180dbaa52...  commit-B: deleted the file
281c084b57...  commit-A: file still existed

In 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 --all

If you want to find commits related to a particular file, you can also use:

git log --all -- path/to/file

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:

Project/deletedFile.cs

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:

git show --name-status <commit-hash>

For example:

git show --name-status e180dbaa52

A deleted file will typically appear with the status:

D    Project/deletedFile.cs

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:

git restore --source=281c084b57... -- Project/deletedFile.cs

The 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.cs

The ^ 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 status

You should see the restored file as a new change:

Changes not staged for commit:
    modified:   Project/deletedFile.cs

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:

git diff -- Project/deletedFile.cs

6. Commit the Restored File

Once you have verified that the file is correct, stage it:

git add Project/deletedFile.cs

Then 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.cs

The file existed in:

E4F5G6H

and was deleted in:

I7J8K9L

You can restore it with:

git restore --source=E4F5G6H -- src/Services/UserService.cs

Or, using the deletion commit’s parent:

git restore --source=I7J8K9L^ -- src/Services/UserService.cs

Then verify:

git status

and 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:

  1. Find the commit where the file was deleted.
  2. Identify the file’s repository-relative path.
  3. 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.

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.