Git: How to get deleted files from previous commit

When you deleted a file and need it back, you can access the file if it was once committed to your git.

  • First, whatever you are doing, make sure to either save and commit or stash any changes for later
  • Find the correct commit that has the file
git log # you can view the commit hash (unique id) and your comments
  • Once the commit you want to go to is identified, copy the hash
# you will check out (go back to) the commit identified as YOUR_HASH
git checkout YOUR_HASH 

# you also have an option to create a new branch from this commit
  • You can move the file outside of the project directory
  • Now, go back to the latest commit (HEAD)
git checkout HEAD