You made a commit that was awesome but you deleted a file that you wanted to keep. Here’s how to restore that deleted file while keeping the other changes in that commit.

  1. Find the commit hash of the commit where you deleted that file using git log.
  2. We shall call the hash 123abc.
  3. If you need to find the path of the file you deleted you can find the list of files affected in the 123abc commit by doing git show 123abc --name-only
  4. Now you know the commit hash and the path to the previously deleted file, we shall call the path images/important.png
  5. Restore with this command: git checkout 123abc~1 images/important.png (Note the ~1 in there, that’s important as it tells Git to use the previous commit to 123abc, in other words, restore this file to the state it was in before this commit hash)

And there you go, you will have the previously deleted file ready to be (re)committed. 🎉