Restoring deleted files in Git

Revision as of 16:05, 10 November 2026 by Planetoid (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Restoring Deleted Files in Git edit

🌐 Switch language: EN, 漢字


Recovery Methods edit

Method 1: Use the Fork Context Menu edit

In the Fork Git client, find the commit that deleted the file.

  1. Right-click the deleted file Deleted.file.
  2. Select Reset File to, then choose the option that matches the commit you selected.
    • State at Commit... restores the version of the file from the selected commit. Choose a commit in which the file still existed.
    • State Before Commit... 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 edit

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 edit

If you deleted the file locally and it still exists in HEAD, restore it directly from HEAD:

git restore --source=HEAD -- 'sub_folder/Deleted.file'

You know a commit in which the file still existed edit

Replace <commit_hash> with the hash of a commit that contains the file. For example, you can use the commit that originally added the file:

git restore --source='<commit_hash>' -- 'sub_folder/Deleted.file'

You only know the commit that deleted the file edit

If the deletion commit has one parent, append ^ to its hash to restore the file from the parent commit:

git restore --source='<commit_hash>^' -- 'sub_folder/Deleted.file'

If the deletion occurred in a merge commit, ^ selects the first parent. Check which parent contains the version you want to restore.

Verify the result and stage the file if needed edit

The git restore --source commands above write the file to the working tree. Review the file contents and check its Git status:

git status --short -- 'sub_folder/Deleted.file'

To include the restored file in your next commit, stage it:

git add -- 'sub_folder/Deleted.file'

Use an older version of Git edit

If your version of Git predates git restore, use git checkout with a source commit and a file path. This form updates both the working tree and the staging area:

git checkout HEAD -- 'sub_folder/Deleted.file'
git checkout '<commit_hash>' -- 'sub_folder/Deleted.file'

Run the command that matches your source. For the second command, replace <commit_hash> with the hash of a commit that contains the file.

Method 3: Restore from Local Changes in Fork edit

If Fork still lists the file as deleted under Local Changes:

  1. Select Local Changes in the sidebar.
  2. Find the deleted file.
  3. Right-click the file and select Discard Changes.

Frequently Asked Questions edit

How do I find the commit for a file deleted a long time ago? edit

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:

git -c core.quotePath=false log --all --full-history --name-status -- ':(glob)**/Deleted.file'

:(glob) enables glob matching. **/ matches zero or more directory levels, while * matches characters within a single path component.

If you are unsure about capitalization, add icase:

git -c core.quotePath=false log --all --full-history --name-status -- ':(glob,icase)**/deleted.file'

In the output, A means added, M means modified, and D 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 git show HEAD --name-only? edit

git show HEAD --name-only lists paths changed by the HEAD commit. A file can still be present in HEAD even if that commit did not change it.

How do I check where the file exists? edit

Check for the file in the working tree:

find . -type f -name 'Deleted.file'

Check whether the current HEAD snapshot contains the path:

git -c core.quotePath=false ls-tree -r --name-only HEAD -- 'sub_folder/Deleted.file'

You can replace HEAD with the hash of any commit you want to check.

Why do Chinese filenames appear as backslashes and numbers? edit

Git may display Chinese filenames using escape sequences. Set core.quotePath=false for a single command to see the actual filenames:

git --no-pager -c core.quotePath=false show --name-only --format= '<commit_hash>'

Use the actual filename in the restore command. To check whether a path exists in the commit's snapshot, use git ls-tree as shown in the previous section.

References edit