Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Restoring deleted files in Git
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
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!
= Restoring Deleted Files in Git = {{LanguageSwitcher | content = [[Restoring deleted files in Git | EN]], [[Restoring deleted files in Git in Mandarin | 漢字]] }} == Recovery Methods == === 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 <code>Deleted.file</code>. # Select '''Reset File to''', then choose the option that matches the commit you selected. #* <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> 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"> git restore --source=HEAD -- 'sub_folder/Deleted.file' </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"> git restore --source='<commit_hash>^' -- 'sub_folder/Deleted.file' </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 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"> git checkout HEAD -- 'sub_folder/Deleted.file' git checkout '<commit_hash>' -- 'sub_folder/Deleted.file' </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'''. == 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"> git -c core.quotePath=false log --all --full-history --name-status -- ':(glob)**/Deleted.file' </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"> git -c core.quotePath=false log --all --full-history --name-status -- ':(glob,icase)**/deleted.file' </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.” === 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 for the file in the working tree: <pre lang="bash"> find . -type f -name 'Deleted.file' </pre> Check whether the current <code>HEAD</code> snapshot contains the path: <pre lang="bash"> git -c core.quotePath=false ls-tree -r --name-only HEAD -- 'sub_folder/Deleted.file' </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 == * [https://git-scm.com/docs/git-restore Git documentation for git-restore] * [https://git-scm.com/docs/git-log Git documentation for git-log] * [https://git-scm.com/docs/git-ls-tree Git documentation for git-ls-tree] * [https://www.git-tower.com/learn/git/faq/restoring-deleted-files How to Restore a Deleted File in 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)
Template used on this page:
Template:LanguageSwitcher
(
edit
)