THE GITHTML FIELD GUIDE
Restore an accidentally deleted tracked file in Git
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.
- 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.
- 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.
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/fileVerify 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.
- 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.
- 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
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.