Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a deleted file in the project commit history?

Tags:

git

Once upon a time, there was a file in my project that I would now like to be able to get.

The problem is: I have no idea of when have I deleted it and on which path it was.

How can I locate the commits of this file when it existed?

like image 883
Pedro Rolo Avatar asked Aug 26 '11 10:08

Pedro Rolo


People also ask

Can you see deleted files on GitHub?

If you have committed the deletion and pushed it to GitHub, it is possible to recover a deleted file using the GitHub Web UI. GitHub lets you browse the commit history and explore the project at any point in history, which then allows you to view and download any file.

How do you restore a file in a commit?

If your commit was used to delete the file you are trying to recover, just use shacommit~1 (ex: git checkout 0f4bbdcd~1 -- path/to/file. txt ) to get the commit immediately before.

How do you see the files changed in a commit?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.


2 Answers

If you do not know the exact path you may use

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

If you know the path the file was at, you can do this:

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

This should show a list of commits in all branches which touched that file. Then, you can find the version of the file you want, and display it with...

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

Or restore it into your working copy with:

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

Note the caret symbol (^), which gets the checkout prior to the one identified, because at the moment of <SHA> commit the file is deleted, we need to look at the previous commit to get the deleted file's contents

like image 80
Amber Avatar answered Sep 28 '22 23:09

Amber


Get a list of the deleted files and copy the full path of the deleted file

git log --diff-filter=D --summary | grep delete 

Execute the next command to find commit id of that commit and copy the commit id

git log --all -- FILEPATH 

Show diff of deleted file

git show COMMIT_ID -- FILE_PATH 

Remember, you can write output to a file using > like

git show COMMIT_ID -- FILE_PATH > deleted.diff 
like image 28
Fatih Acet Avatar answered Sep 28 '22 23:09

Fatih Acet