Git restore deleted file: the command for each case
For a git restore deleted file job, first find your case. If the deletion is not committed, run git restore <file> to get it back from the index. If the deletion is committed, use git restore --source with an earlier commit, branch or tag. If you never committed the file, Git has no copy to restore from.
Something is gone from your folder and you want it back. Git can often do it in one line, but the right line depends on where the file went missing. There are four cases, and the table below sorts them. Each git restore deleted file command here changes your working tree, and some change the index too. Git always copies from a source: the index, a commit, a branch or a tag.
01Terms you need
Four words come up on this page. The working tree is the files in your project folder, as you see them. The index, also called the staging area, is the list of changes you have added for the next commit. A commit is a saved snapshot in Git's history. HEAD is the commit you currently have checked out, the latest one on your branch.
02Choose your case
| Your case | Command to use |
|---|---|
| 1. The file is gone from the folder, and you did not stage or commit the deletion | git restore <file> |
| 2. The deletion was staged, meaning added to the index for the next commit | git restore --source=HEAD --staged --worktree <file> |
| 3. The deletion was committed | git restore --source <commit> <file> |
4. You moved a branch back with git reset --hard and files look gone | The reflog, see undo a Git reset |
A fifth situation is not a case but a dead end: the file was never committed. That is covered in its own section below.
03Which case are you in?
Think about what happened to the deletion. Did you or an agent only remove the file from the folder? That is the first case. Did someone stage the removal, so it waits in the index? That is the second case. Did a commit record the removal? That is the third case. Did a reset move your branch and make recent commits disappear? That is the fourth case.
If you cannot tell which git restore deleted file case applies, ask Git. This command only reads and changes nothing:
git statusIt lists staged changes, unstaged changes and untracked files. If your file is among the unstaged changes, you are in the first case. If it is among the staged changes, you are in the second. If it does not appear at all and is missing from the folder, the deletion was committed or a reset moved the branch, so look at the third and fourth cases.
04Case 1: deleted but not committed
Use this when you removed the file from the folder and have not staged or committed the deletion. It restores the file from the index, which is how a file deleted by mistake gets back. By default, the working tree is what gets restored. Warning: if you had unstaged edits to that same path, the index copy replaces them.
git restore <file>Replace <file> with the real path.
To restore everything in the current directory, there is a shorter form. Warning: it overwrites unstaged work in that whole directory with the index versions, so use it only when you do not need those edits.
git restore .05Case 2: the deletion is staged
If the deletion is staged, the index no longer holds the file, so git restore <file> has nothing to copy from. Restore both the index and the working tree from HEAD. Use it when you want the file back as if the deletion never happened. Warning: it rewrites both places, so unstaged edits to that path are replaced.
git restore --source=HEAD --staged --worktree <file>Git also says that with --staged, the contents are restored from HEAD, and otherwise from the index. The same page shows git restore --staged <file> for the smaller job of unstaging a file.
06Case 3: the deletion is committed
Use this when the commit that removed the file is already made. First find a commit that still had the file. This command lists the commits that changed it, newest first, even across a rename:
git log --follow path/to/filePick the commit just before the one that deleted it. master~2 below means "two commits before the tip of master". Name a commit, branch or tag that still has the file, and Git copies that version into your working tree. Warning: the file in your folder is overwritten with that version, so any current edits to it are lost. For example:
git restore --source master~2 MakefileHere master~2 is the source and Makefile is the path. Put your own commit, branch or tag and your own path in their place. A source that is too old brings back an old version of the path, not the last one you wrote, so open the result and check it. A file deleted in GitHub's web UI works the same way, because the data stays in the repository's Git history.
If you prefer a record to an overwrite, git revert records new commits that reverse earlier ones. The docs' example reverts the fourth last commit and creates a new commit with the reversed changes:
git revert HEAD~3Put the hash of the commit that deleted the file in place of HEAD~3.
07Case 4: after git reset
A hard reset can make committed work look deleted. Warning: git reset --hard overwrites all files and directories with the version from the commit you name, and may overwrite untracked files, which are files Git has never tracked. Do not try to restore file by file. The reflog, Git's log of where your branch tips and other references used to be, records when they were updated in the local repository. So it can show where the branch was before the reset. The walk-through, with the exact commands and how long entries last, is on undo a Git reset.
08When Git has nothing to restore
Git restores from a source, and a file that was never added has none. Pro Git says that when untracked items are removed, "there is often no retrieving the content of those files". Check the Trash and your backups; the steps are on recover deleted files on a Mac. The untracked files page goes through the options. If an AI agent ran the delete, Claude Code deleted files covers the first ten minutes.
09Git restore deleted file: what to remember
Find your case first. Not committed: git restore <file>. Staged: restore from HEAD with --staged --worktree. Committed: git restore --source with a commit, branch or tag. After a reset: the reflog. Never committed: the Trash or a backup.
Commit before you let an agent loose on a repo. Claude Code's checkpoints do not track a file that only Bash commands changed, so you revert it with Git; see Claude Code undo and Claude Code checkpoints, and Cursor restore checkpoint for the Cursor side.
FAQ
How do I undo a git delete?
If the deletion is not committed, run git restore <file>, which gets the file back from the index. If the deletion is committed, restore the file from an earlier commit with git restore --source.
How do I restore all the deleted files?
To restore all files in the current directory, Git documents git restore .. It restores from the index by default, so unstaged edits in those files are replaced. Run it only when you do not need them.
Can I retrieve a file I accidentally deleted?
If Git tracked it, yes. Git restores specified paths in the working tree from a restore source. If the file was never committed, Git has nothing to restore it from; check the Trash or a backup.
How do I restore a deleted file after git reset --hard?
Look at the reflog, which records when branch tips were updated, and move the branch back to its earlier position. The steps are on the undo-reset page.
How to regain permanently deleted files?
Git can bring back only what it tracked. For other files, check the Trash and your backups; the steps are on the Mac recovery page.
Can I find a deleted file in the Git history?
Yes, if it was committed. A file deleted in GitHub's web UI still remains in the repository's Git history. Pick a commit, branch or tag that still has the file and use it as the source.