Editing
Restoring deleted files in Git
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
= Recovering GIT Accidentally Deleted Files = {{LanguageSwitcher | content = [[Restoring deleted files in Git | EN]], [[Restoring deleted files in Git in Mandarin | 漢字]] }} == Recovery Methods == === Method 1: Via Fork Right-Click Menu === Using [https://git-fork.com/ Fork Git client], right-click on <code>Deleted.file</code> and select '''Reset File to'''. # Right-click the deleted file # Select '''Reset File to''' → <code>State at Commit...</code> or <code>State Before Commit...</code> * <code>State at Commit...</code> → Restore to the state at a specific commit * <code>State Before Commit...</code> → Restore to the state before that commit > '''Tip:''' Use the menu directly and click <code>State Before Commit...</code> to quickly restore the file to its state before deletion. === Method 2: Command Line === > '''Note:''' The file path must match exactly (case-sensitive, directory levels must be consistent). # Restore from the latest commit: <pre lang="bash"> git checkout HEAD -- sub_folder/Deleted.file </pre> # Or restore from a specific commit (using commit hash): <pre lang="bash"> git checkout <commit_hash> -- sub_folder/Deleted.file </pre> === Method 3: Restore from Local Changes === If the file still appears as deleted in '''Local Changes''': # Click '''Local Changes''' in the left panel # Locate the file # Right-click → '''Discard Changes''' == Frequently Asked Questions == === A file was deleted long ago and I can't quickly find which commit hash deleted it in Fork === If you remember the filename (case must match, but you're unsure which directory it's in): <pre lang="bash"> git log --all --full-history -- "**/Deleted.file" </pre> Explanation of <code>**</code> and <code>*</code> wildcards: # <code>**</code> matches any number of directory levels (including zero) # <code>*</code> matches only a single directory level or any characters within a filename When unsure about path depth, use <code>**</code>. If you're unsure about the filename's case, use <code>grep -i</code> for case-insensitive search: <pre lang="bash"> git log --all --full-history --name-only --format="" | grep -i "deleted.file" </pre> If the result appears N times, it means the file has been modified N times in Git history and has many versions to restore from. Once you find the correct path, use the full exact path to restore it. To find the commit hash to restore from: <pre lang="bash"> git log --all --full-history -- "**/Deleted.file" </pre> === Why does <code>git show HEAD --name-only | grep Deleted.file</code> return no results? === <code>git show HEAD --name-only</code> only lists '''files changed in the most recent commit''', not all files — so the file not appearing doesn't mean it doesn't exist. Possible reasons: * The file hasn't been modified recently and the latest commit didn't touch it * The file has already been deleted * The path is incorrect === First, confirm where the file exists === Check whether it currently exists in the working tree: <pre lang="bash"> find . -name "Deleted.file" </pre> Check whether the file exists in the current HEAD (regardless of whether it's been modified): <pre lang="bash"> git ls-files | grep Deleted.file </pre> == References == * [https://git-scm.com/docs/git-restore Git Official Docs - git-restore] * [https://git-scm.com/docs/git-log Git Official Docs - git-log] * [https://www.git-tower.com/learn/git/faq/restoring-deleted-files How to Restore a Deleted File in Git - git-tower] * [https://rewind.com/blog/recovering-deleted-files-in-github/ Recovering Deleted Files in GitHub - Rewind] * [https://www.smashingmagazine.com/2023/12/recovering-deleted-files-git-working-tree/ Recovering Deleted Files From Your Git Working Tree - Smashing Magazine] * [https://www.atlassian.com/git/tutorials/git-log Advanced Git Log - Atlassian] * [https://github.blog/open-source/git/gits-database-internals-iii-file-history-queries/ Git's database internals III: file history queries - GitHub Blog] [[Category: Git]] [[Category: Revised with LLMs]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information