Restoring deleted files in Git: Difference between revisions
No edit summary Tag: wikieditor |
mNo edit summary |
||
| Line 1: | Line 1: | ||
= | = Restoring Deleted Files in Git = | ||
{{LanguageSwitcher | content = [[Restoring deleted files in Git | EN]], [[Restoring deleted files in Git in Mandarin | 漢字]] }} | {{LanguageSwitcher | content = [[Restoring deleted files in Git | EN]], [[Restoring deleted files in Git in Mandarin | 漢字]] }} | ||
| Line 5: | Line 5: | ||
== Recovery Methods == | == Recovery Methods == | ||
=== Method 1: | === Method 1: Use the Fork Context Menu === | ||
In the [https://git-fork.com/ Fork Git client], find the commit that deleted the file. | |||
# Right-click the deleted file | # Right-click the deleted file <code>Deleted.file</code>. | ||
# Select '''Reset File to''' | # Select '''Reset File to''', then choose the option that matches the commit you selected. | ||
* <code>State at Commit...</code> | #* <code>State at Commit...</code> restores the version of the file from the selected commit. Choose a commit in which the file still existed. | ||
* <code>State Before Commit...</code> | #* <code>State Before Commit...</code> 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 <code>HEAD</code>, restore it directly from <code>HEAD</code>: | |||
<pre lang="bash"> | <pre lang="bash"> | ||
git | git restore --source=HEAD -- 'sub_folder/Deleted.file' | ||
</pre> | </pre> | ||
==== You know a commit in which the file still existed ==== | |||
Replace <code><commit_hash></code> with the hash of a commit that contains the file. For example, you can use the commit that originally added the file: | |||
<pre lang="bash"> | |||
git restore --source='<commit_hash>' -- 'sub_folder/Deleted.file' | |||
</pre> | |||
==== You only know the commit that deleted the file ==== | |||
If the deletion commit has one parent, append <code>^</code> to its hash to restore the file from the parent commit: | |||
<pre lang="bash"> | <pre lang="bash"> | ||
git | git restore --source='<commit_hash>^' -- 'sub_folder/Deleted.file' | ||
</pre> | </pre> | ||
=== | If the deletion occurred in a merge commit, <code>^</code> selects the first parent. Check which parent contains the version you want to restore. | ||
==== Verify the result and stage the file if needed ==== | |||
The <code>git restore --source</code> commands above write the file to the working tree. Review the file contents and check its Git status: | |||
<pre lang="bash"> | |||
git status --short -- 'sub_folder/Deleted.file' | |||
</pre> | |||
= | To include the restored file in your next commit, stage it: | ||
<pre lang="bash"> | |||
git add -- 'sub_folder/Deleted.file' | |||
</pre> | |||
=== | ==== Use an older version of Git ==== | ||
If | If your version of Git predates <code>git restore</code>, use <code>git checkout</code> with a source commit and a file path. This form updates both the working tree and the staging area: | ||
<pre lang="bash"> | <pre lang="bash"> | ||
git | git checkout HEAD -- 'sub_folder/Deleted.file' | ||
git checkout '<commit_hash>' -- 'sub_folder/Deleted.file' | |||
</pre> | </pre> | ||
Run the command that matches your source. For the second command, replace <code><commit_hash></code> 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'''. | |||
If you | == 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: | |||
<pre lang="bash"> | <pre lang="bash"> | ||
git log --all --full-history --name- | git -c core.quotePath=false log --all --full-history --name-status -- ':(glob)**/Deleted.file' | ||
</pre> | </pre> | ||
<code>:(glob)</code> enables glob matching. <code>**/</code> matches zero or more directory levels, while <code>*</code> matches characters within a single path component. | |||
If you are unsure about capitalization, add <code>icase</code>: | |||
<pre lang="bash"> | <pre lang="bash"> | ||
git log --all --full-history -- | git -c core.quotePath=false log --all --full-history --name-status -- ':(glob,icase)**/deleted.file' | ||
</pre> | </pre> | ||
In the output, <code>A</code> means added, <code>M</code> means modified, and <code>D</code> 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.” | |||
<code>git show HEAD --name-only</code> | === Why can't I find the file with <code>git show HEAD --name-only</code>? === | ||
<code>git show HEAD --name-only</code> lists paths changed by the <code>HEAD</code> commit. A file can still be present in <code>HEAD</code> even if that commit did not change it. | |||
=== | === How do I check where the file exists? === | ||
Check | Check for the file in the working tree: | ||
<pre lang="bash"> | <pre lang="bash"> | ||
find . -name | find . -type f -name 'Deleted.file' | ||
</pre> | </pre> | ||
Check whether | Check whether the current <code>HEAD</code> snapshot contains the path: | ||
<pre lang="bash"> | <pre lang="bash"> | ||
git ls- | git -c core.quotePath=false ls-tree -r --name-only HEAD -- 'sub_folder/Deleted.file' | ||
</pre> | </pre> | ||
You can replace <code>HEAD</code> 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 <code>core.quotePath=false</code> for a single command to see the actual filenames: | |||
<pre lang="bash"> | |||
git --no-pager -c core.quotePath=false show --name-only --format= '<commit_hash>' | |||
</pre> | |||
Use the actual filename in the restore command. To check whether a path exists in the commit's snapshot, use <code>git ls-tree</code> as shown in the previous section. | |||
== References == | == References == | ||
* [https://git-scm.com/docs/git-restore Git | * [https://git-scm.com/docs/git-restore Git documentation for git-restore] | ||
* [https://git-scm.com/docs/git-log Git | * [https://git-scm.com/docs/git-log Git documentation for git-log] | ||
* [https://www.git-tower.com/learn/git/faq/restoring-deleted-files How to Restore a Deleted File in Git | * [https://git-scm.com/docs/git-ls-tree Git documentation for git-ls-tree] | ||
* [https://rewind.com/blog/recovering-deleted-files-in-github/ Recovering Deleted Files in GitHub | * [https://www.git-tower.com/learn/git/faq/restoring-deleted-files How to Restore a Deleted File in Git – Tower] | ||
* [https://www.smashingmagazine.com/2023/12/recovering-deleted-files-git-working-tree/ Recovering Deleted Files From Your Git Working Tree | * [https://rewind.com/blog/recovering-deleted-files-in-github/ Recovering Deleted Files in GitHub – Rewind] | ||
* [https://www.atlassian.com/git/tutorials/git-log Advanced Git Log | * [https://www.smashingmagazine.com/2023/12/recovering-deleted-files-git-working-tree/ Recovering Deleted Files From Your Git Working Tree – Smashing Magazine] | ||
* [https://github.blog/open-source/git/gits-database-internals-iii-file-history-queries/ Git's | * [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: Git]] | ||
[[Category: Revised with LLMs]] | [[Category: Revised with LLMs]] | ||