3

I have occasionally delete some file from the project under git control. After that I made many commits. How to (1) find latest revision in the history, where this file contains and (2) restore only it?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

2 Answers2

4

To find the commit that deleted the file:

git log --all -- folder/other-folder/file_name.txt

or even just

git log --all -- */file_name.txt

Then just do this:

git checkout (commit hash)^ -- folder/other-folder/file_name.txt

The ^ tells the git to use not that commit, but one prior.

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
0

If you can identify which commit the deletion took place in, you can try git reflog. reflog will show you all the commits that were made to the repository. Once you find the commit, you can do a git checkout <SHA of commit above>, that will take your working directory to the state of that commit. You can then copy that version of the file manually, do a git checkout <branchname> to go to the required branch and then commit the addition of the file in the next commit.

gravetii
  • 9,273
  • 9
  • 56
  • 75