# Restore an accidentally deleted tracked file in Git

Canonical: https://githtml.com/guides/git-restore-accidentally-deleted-file
Published: 2026-11-23
Publisher: WaveTech LLC / gitHtml

To recover a deleted tracked file, first identify whether its deletion is unstaged, staged, or already committed. Choose the source snapshot deliberately and restore only that path. Restoring from HEAD can overwrite current content, so preserve any replacement file or local edits first. Untracked files require a different backup-based recovery path.

## Identify where the wanted version still exists

If you deleted a tracked file in the working tree but did not stage the deletion, the index may still contain the desired version. If you staged the deletion, HEAD may contain it. If the deletion was committed, find a verified earlier commit where the path existed. These are different recovery sources, and choosing the wrong one can restore an outdated file.

Inspect status and path-specific history before acting. A renamed file may not truly be lost; search the repository and review the deletion commit. Also check whether another local file now occupies the same path. Restoring content over that replacement without a copy can turn one recovery problem into another.

## Restore one path from a known snapshot

The example restores the HEAD version into the working tree only. It is appropriate when HEAD contains the wanted file and replacing the working path is intended. If the deletion is staged, the index remains a separate consideration; inspect it before deciding whether to stage the restored file. Replace the path with the exact tracked filename.

1. For a committed deletion, inspect a known earlier commit’s file content and use that explicit commit as the restore source rather than moving the entire branch backward.
2. If the file was never tracked or saved in a stash, check editor history, backups, or filesystem recovery options; ordinary Git history may not contain it.

````shell
git status --short -- path/to/file
git log -5 --oneline -- path/to/file
git show HEAD:path/to/file
git restore --source=HEAD --worktree -- path/to/file
git status --short -- path/to/file
````

## Verify content and intended tracking state

Open the recovered file and compare it with the version you actually needed. Run the relevant parser, test, or build check if the file affects behavior. A recovered configuration from months ago may exist but be incompatible with the current project.

Decide whether recovery should become a new commit or simply cancel an uncommitted deletion. Inspect the staged diff to ensure it expresses that intent. Keep the original source commit identifier in the recovery note when restoring historical content, because future reviewers need to know whether the file was recovered verbatim or adapted for the current code.

1. If the file had staged improvements before its working copy was deleted, inspect the index version before choosing HEAD. Restoring the older committed snapshot could omit those improvements. The safest source is the snapshot containing the desired content, not automatically the repository’s latest commit.
2. For a file deleted intentionally in an earlier release, read that deletion’s rationale before reviving it. Its former imports or configuration references may no longer exist. Restore the historical content into a reviewed change and adapt the necessary connections rather than assuming file recovery also restores supported functionality.

## Sources and further reading

- [Git reference: git-restore](https://git-scm.com/docs/git-restore)
- [Git reference: git-show](https://git-scm.com/docs/git-show)
- [Git reference: git-log](https://git-scm.com/docs/git-log)

## Related guides

- [Resolve or abort a conflicting Git revert](https://githtml.com/guides/git-revert-conflict-resolve-or-abort)
- [Fix “src refspec does not match any” without guessing](https://githtml.com/guides/git-src-refspec-does-not-match-any)
- [Fill an empty submodule directory after cloning](https://githtml.com/guides/git-submodule-directory-empty-after-clone)
- [Move uncommitted work to the right Git branch](https://githtml.com/guides/git-uncommitted-changes-wrong-branch)

Editorial approach: https://githtml.com/guides/about
