How to Recover a Deleted File in Git
You can often restore a deleted file from the index or a previous commit. The correct command depends on whether you staged or committed the deletion. Check the file status first so you do not overwrite other changes.
Practice Git workflows →Check whether the deletion is staged
Start by checking the working tree and index:
git status --shortA deletion marked in the first status column is staged; one in the second column is an unstaged working-tree deletion. If the file has new edits or other changes, review its diff before restoring it.
Restore an unstaged deletion
To restore a tracked file deleted from the working tree to the version in the index, run:
git restore -- path/to/fileReplace path/to/file with the repository-relative path. This restores the index version, so any uncommitted edits to that file that are not in the index can be lost. Review or save those edits first.
Unstage a deletion and restore the file
If you staged the deletion with git rm or git add, first unstage it, then restore the working-tree copy:
git restore --staged -- path/to/file
git restore -- path/to/fileThe first command restores the index entry from HEAD; the second restores the file in your working tree. Inspect git status after each step to confirm the result.
Recover a file deleted in an earlier commit
To restore a file from a specific commit without changing other files, first find the commit where the file existed:
git log --all --oneline -- path/to/fileThen restore that file from the chosen commit into your working tree:
git restore --source=<commit> -- path/to/fileReplace <commit> with a commit hash from the log. Review the restored file and then stage it if you want to commit the recovery. The command restores only the named path, not the entire historical commit.
If the file was never committed
Git can only restore content it knows about, such as a committed version or a staged snapshot. If the file was untracked and never staged, Git does not have a copy to restore. Check your editor’s local history, system backups, or other recovery options.
Deleted-file recovery FAQ
Does git restore delete my other files?
When you specify a path, git restore acts on that path. Avoid omitting the path if you only intend to recover one file, and check git status before and after the command.
Can Git recover a file that was never committed?
Only if the content was staged or otherwise recorded in a Git commit. Untracked, unstaged content is not stored in Git history.