Restoring deleted files in Git: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
 
Line 1: Line 1:
= 復原 GIT 誤刪除的檔案 =
= Recovering GIT Accidentally Deleted Files =


== 復原方法 ==
{{LanguageSwitcher | content = [[Restoring deleted files in Git | EN]], [[Restoring deleted files in Git in Mandarin | 漢字]] }}


=== 方法一:透過 Fork 右鍵選單 ===
== Recovery Methods ==


使用 [https://git-fork.com/ Fork Git 客戶端軟體],並且對 <code>Deleted.file</code> 右鍵選擇了 '''Reset File to'''。
=== 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'''.
# 選擇 '''Reset File to''' → <code>State at Commit...</code> 或 <code>State Before Commit...</code>
#* <code>State at Commit...</code> → 還原到某個特定 commit 的狀態
#* <code>State Before Commit...</code> → 還原到該 commit 之前的狀態


> '''建議:''' 直接用選單,點 <code>State Before Commit...</code> 即可快速還原到刪除前的狀態。
# 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 ===


1. 從最新 commit 還原
> '''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>


2. 或從特定 commit 還原(用 commit hash)
# 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>


=== 方法三:從 Local Changes 還原 ===
=== Method 3: Restore from Local Changes ===


如果檔案還在 '''Local Changes''' 裡顯示為已刪除:
If the file still appears as deleted in '''Local Changes''':


# 點左側 '''Local Changes'''
# Click '''Local Changes''' in the left panel
# 找到該檔案
# Locate the file
# 右鍵 → '''Discard Changes'''
# Right-click → '''Discard Changes'''


== 常見問題 ==
== Frequently Asked Questions ==


=== 很久前刪除的檔案,無法從 Fork 快速找到刪除檔案的哪一筆紀錄 commit hash ===
=== 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> <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


不確定路徑深度時建議用 <code>**</code>
When unsure about path depth, use <code>**</code>.


如果不確定檔名大小寫,用 <code>grep -i</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>


出現 N 次,代表這個檔案在 GIT 歷史中被修改過 N 次,有很多版本可以還原。找到正確路徑後,再用完整正確路徑去還原。
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.


找到要復原的 commit hash:
To find the commit hash to restore from:


<pre lang="bash">
<pre lang="bash">
Line 66: Line 68:
</pre>
</pre>


=== 為什麼 <code>git show HEAD --name-only | grep Deleted.file</code> 沒有結果? ===
=== Why does <code>git show HEAD --name-only | grep Deleted.file</code> return no results? ===


<code>git show HEAD --name-only</code> 只會列出'''最新那一筆 commit 有變動的檔案''',不是所有檔案,所以沒出現不代表不存在。
<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:
* 這個檔案很久沒被修改,最近的 commit 根本沒動到它
* 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 ===


看它現在存不存在於 working tree:
Check whether it currently exists in the working tree:


<pre lang="bash">
<pre lang="bash">
Line 83: Line 85:
</pre>
</pre>


看目前 HEAD 裡有沒有這個檔案(不管有沒有被修改):
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 官方文件 - git-restore]
* [https://git-scm.com/docs/git-restore Git Official Docs - git-restore]
* [https://git-scm.com/docs/git-log Git 官方文件 - git-log]
* [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]

🌐 Switch language: EN, 漢字


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.

  1. Right-click the deleted file
  2. Select Reset File toState at Commit... or State Before Commit...
  • State at Commit... → Restore to the state at a specific commit
  • State 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).

  1. Restore from the latest commit:
git checkout HEAD -- sub_folder/Deleted.file
  1. 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:

  1. Click Local Changes in the left panel
  2. Locate the file
  3. 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:

  1. ** matches any number of directory levels (including zero)
  2. * 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]