Restoring a deleted file

We can restore files that we deleted in previous commits. Suppose that we accidentally removed app.py and now we need it back. The first thing that we need to do is find the last commit where that file was not deleted. To do that we run

git log --oneline -- app.py

So far nothing new. Now, since the file will show up on the commit in which we delete it, the last commit were the file was not deleted is the second commit from the top: commit 8eec1d2. What we need to do now is to git checkout just that file from that commit. So we run

git checkout 8eec1d2 app.py

Git will take the file as it was at that point in time and place it in our staging area. That is why if we run a git status -s we can see that the file has an A on the left column. Lastly, we commit the file to the repository.

Restoring a deleted file 1