Restoring deleted files in Git
Restoring Deleted Files in Git
Recovery Methods
Method 1: Use the Fork Context Menu
In the Fork Git client, find the commit that deleted the file.
- Right-click the deleted file
Deleted.file. - Select Reset File to, then choose the option that matches the commit you selected.
State at Commit...restores the version of the file from the selected commit. Choose a commit in which the file still existed.State Before Commit...restores the file as it was before the selected commit. Use this option when you select the commit that deleted the file.
Method 2: Use the Command Line
Run these commands from the repository root. The file path must match the path in the source commit, including capitalization and directory structure. If you have uncommitted changes at the same path, back them up first because restoring the file will overwrite them.
The file still exists in HEAD
If you deleted the file locally and it still exists in HEAD, restore it directly from HEAD:
git restore --source=HEAD -- 'sub_folder/Deleted.file'
You know a commit in which the file still existed
Replace <commit_hash> with the hash of a commit that contains the file. For example, you can use the commit that originally added the file:
git restore --source='<commit_hash>' -- 'sub_folder/Deleted.file'
You only know the commit that deleted the file
If the deletion commit has one parent, append ^ to its hash to restore the file from the parent commit:
git restore --source='<commit_hash>^' -- 'sub_folder/Deleted.file'
If the deletion occurred in a merge commit, ^ selects the first parent. Check which parent contains the version you want to restore.
Verify the result and stage the file if needed
The git restore --source commands above write the file to the working tree. Review the file contents and check its Git status:
git status --short -- 'sub_folder/Deleted.file'
To include the restored file in your next commit, stage it:
git add -- 'sub_folder/Deleted.file'
Use an older version of Git
If your version of Git predates git restore, use git checkout with a source commit and a file path. This form updates both the working tree and the staging area:
git checkout HEAD -- 'sub_folder/Deleted.file'
git checkout '<commit_hash>' -- 'sub_folder/Deleted.file'
Run the command that matches your source. For the second command, replace <commit_hash> with the hash of a commit that contains the file.
Method 3: Restore from Local Changes in Fork
If Fork still lists the file as deleted under Local Changes:
- Select Local Changes in the sidebar.
- Find the deleted file.
- Right-click the file and select Discard Changes.
Frequently Asked Questions
How do I find the commit for a file deleted a long time ago?
If you remember the filename but have forgotten its directory, search the history reachable from your local Git references. The following command lists matching commits, file paths, and change types:
git -c core.quotePath=false log --all --full-history --name-status -- ':(glob)**/Deleted.file'
:(glob) enables glob matching. **/ matches zero or more directory levels, while * matches characters within a single path component.
If you are unsure about capitalization, add icase:
git -c core.quotePath=false log --all --full-history --name-status -- ':(glob,icase)**/deleted.file'
In the output, A means added, M means modified, and D means deleted. Once you find the version you want, use the full path shown in the output. If you found a deletion entry, follow the steps under “You only know the commit that deleted the file.”
Why can't I find the file with git show HEAD --name-only?
git show HEAD --name-only lists paths changed by the HEAD commit. A file can still be present in HEAD even if that commit did not change it.
How do I check where the file exists?
Check for the file in the working tree:
find . -type f -name 'Deleted.file'
Check whether the current HEAD snapshot contains the path:
git -c core.quotePath=false ls-tree -r --name-only HEAD -- 'sub_folder/Deleted.file'
You can replace HEAD with the hash of any commit you want to check.
Why do Chinese filenames appear as backslashes and numbers?
Git may display Chinese filenames using escape sequences. Set core.quotePath=false for a single command to see the actual filenames:
git --no-pager -c core.quotePath=false show --name-only --format= '<commit_hash>'
Use the actual filename in the restore command. To check whether a path exists in the commit's snapshot, use git ls-tree as shown in the previous section.
References
- Git documentation for git-restore
- Git documentation for git-log
- Git documentation for git-ls-tree
- How to Restore a Deleted File in Git – Tower
- Recovering Deleted Files in GitHub – Rewind
- Recovering Deleted Files From Your Git Working Tree – Smashing Magazine
- Advanced Git Log – Atlassian
- Git's Database Internals III: File History Queries – GitHub Blog