in Technology by
How to find and restore a deleted file in a Git repository?

1 Answer

0 votes
by

Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Or in one command, if $file is the file in question.

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

If you are using zsh and have the EXTENDED_GLOB option enabled, the caret symbol won't work. You can use ~1 instead.

git checkout $(git rev-list -n 1 HEAD -- "$file")~1 -- "$file"

Related questions

0 votes
    Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some ... my original project checkout. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How do you push a file from your local system to the GitHub repository using Git?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    Can you recover a deleted branch in Git?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    1. When you delete a file from a hard disk, the deleted file is stored in the Recycle Bin where you can ... Recycle Bin (b) Computer Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    How to push a new local branch to a remote Git repository and track it too?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    On adding a subproject using git submodule add, how many files will be added in the main repository? (1)4 (2)1 (3)2 (4)3...
asked Jun 1, 2021 in Technology by JackTerrance
0 votes
    How can we clone a Git repository into a specific folder?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    Explain How to change the URI (URL) for a remote Git repository?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to determine the URL that a local Git repository was originally cloned from?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to add an empty directory to a Git repository?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to resolve merge conflicts in Git repository?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to revert a Git repository to a previous commit?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How is a bare repository different from the standard way of initializing a Git repository?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    Microsoft now has support for Git repositories on their Team Foundation Service. I have an account on Team ... to TFService. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I pulled a project from GitHub a few days ago. I've since discovered that there are several forks on GitHub, ... those forks I pulled? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
...