How to Find a Deleted File in the Project Commit History

There are situations when you have deleted a file in the project, and now you want it to get back. We suggest a solution to this issue in the tutorial.

If you delete the file and do not know on which path it was at then you should execute this command line:

git log --all --full-history -- "**/thisfile.*"

If you know the file path then run the following:

git log --all --full-history -- <path-to-file>

Running the above command should show a list of commits in all branches which connected that file. Now you can find the specified version of the file and display it with:

git show <SHA> -- <path-to-file>

For restoring it into your working copy using the git checkout command:

git checkout <SHA>^ -- <path-to-file>

The caret symbol (^) gets the checkout before the identified one. At the moment of <SHA> commit, the file is deleted, you need to look at the previous commit to get the contents of the deleted files.

The git log Command

The git log command shows committed snapshots used for listing and filtering the project history and searching for particular changes. It is a tool used for examining a repository’s history and finding a particular version of a project. The --alloption shows all the commits in the history of branches, tags, and other references. The --full-history option simplifies the history explaining the final state of the tree.

The git checkout Command

The git checkout command is used for switching branches or restoring working tree files. It operates on files, commits, and branches. It allows switching between multiple features in just a single repository. The git checkout command works with the git branch command. It updates the files in the working directory to match the version stored in that branch, instructing Git to record all the new commits.

Do you find this helpful?

Related articles