Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.

I know I can check out a file using 

git checkout HEAD^ foo.bar

, but I don't know when that file was deleted.

  1. What would be the quickest way to find the commit that deleted a given filename?
  2. What would be the easiest way to get that file back into my working copy?

I'm hoping I don't have to manually browse my logs, check out the entire project for a given SHA and then manually copy that file into my original project checkout.

1 Answer

0 votes
by (50.2k points)

To resolve this issue first we need to find the last commit that affected the given path and delete it, for deleting follow the commands

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

To checkout the version before commit

git checkout <deleting_commit>^ -- <file_path>

If you are using zsh and have the EXTENDED_GLOB option enabled then use 

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

It will restore a deleted repository in the git repository.

Browse Categories

...