Restoring deleted files in Git: Difference between revisions
m (→方法二:命令列) |
No edit summary |
||
| Line 1: | Line 1: | ||
= | = 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"> | <pre lang="bash"> | ||
git checkout HEAD -- sub_folder/Deleted.file | git checkout HEAD -- sub_folder/Deleted.file | ||
</pre> | </pre> | ||
# Or restore from a specific commit (using commit hash): | |||
<pre lang="bash"> | <pre lang="bash"> | ||
git checkout <commit_hash> -- sub_folder/Deleted.file | git checkout <commit_hash> -- sub_folder/Deleted.file | ||
</pre> | </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"> | <pre lang="bash"> | ||
| Line 46: | Line 48: | ||
</pre> | </pre> | ||
<code>**</code> | Explanation of <code>**</code> and <code>*</code> wildcards: | ||
# <code>**</code> | # <code>**</code> matches any number of directory levels (including zero) | ||
# <code>*</code> | # <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"> | <pre lang="bash"> | ||
| Line 58: | Line 60: | ||
</pre> | </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"> | <pre lang="bash"> | ||
| Line 66: | Line 68: | ||
</pre> | </pre> | ||
=== | === Why does <code>git show HEAD --name-only | grep Deleted.file</code> return no results? === | ||
<code>git show HEAD --name-only</code> | <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"> | <pre lang="bash"> | ||
| Line 83: | Line 85: | ||
</pre> | </pre> | ||
Check whether the file exists in the current HEAD (regardless of whether it's been modified): | |||
<pre lang="bash"> | <pre lang="bash"> | ||
| Line 89: | Line 91: | ||
</pre> | </pre> | ||
== | == References == | ||
* [https://git-scm.com/docs/git-restore Git | * [https://git-scm.com/docs/git-restore Git Official Docs - git-restore] | ||
* [https://git-scm.com/docs/git-log Git | * [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://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://rewind.com/blog/recovering-deleted-files-in-github/ Recovering Deleted Files in GitHub - Rewind] | ||
| Line 98: | Line 100: | ||
* [https://www.atlassian.com/git/tutorials/git-log Advanced Git Log - Atlassian] | * [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] | * [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: Git]] | ||
[[Category: Revised with LLMs]] | [[Category: Revised with LLMs]] | ||
Latest revision as of 09:30, 6 March 2026
Recovering GIT Accidentally Deleted Files[edit]
Recovery Methods[edit]
Method 1: Via Fork Right-Click Menu[edit]
Using Fork Git client, right-click on Deleted.file and select Reset File to.
- Right-click the deleted file
- Select Reset File to →
State at Commit...orState Before Commit...
State at Commit...→ Restore to the state at a specific commitState Before Commit...→ Restore to the state before that commit
> Tip: Use the menu directly and click State Before Commit... to quickly restore the file to its state before deletion.
Method 2: Command Line[edit]
> Note: The file path must match exactly (case-sensitive, directory levels must be consistent).
- Restore from the latest commit:
git checkout HEAD -- sub_folder/Deleted.file
- Or restore from a specific commit (using commit hash):
git checkout <commit_hash> -- sub_folder/Deleted.file
Method 3: Restore from Local Changes[edit]
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[edit]
A file was deleted long ago and I can't quickly find which commit hash deleted it in Fork[edit]
If you remember the filename (case must match, but you're unsure which directory it's in):
git log --all --full-history -- "**/Deleted.file"
Explanation of ** and * wildcards:
**matches any number of directory levels (including zero)*matches only a single directory level or any characters within a filename
When unsure about path depth, use **.
If you're unsure about the filename's case, use grep -i for case-insensitive search:
git log --all --full-history --name-only --format="" | grep -i "deleted.file"
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:
git log --all --full-history -- "**/Deleted.file"
Why does git show HEAD --name-only | grep Deleted.file return no results?[edit]
git show HEAD --name-only 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[edit]
Check whether it currently exists in the working tree:
find . -name "Deleted.file"
Check whether the file exists in the current HEAD (regardless of whether it's been modified):
git ls-files | grep Deleted.file
References[edit]
- Git Official Docs - git-restore
- Git Official Docs - git-log
- How to Restore a Deleted File in Git - 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