how to recover deleted file with git - 09-12-23

I’d like to build a new nest.js command based off a file I deleted last week. Here is how I go about getting that old file back via git restore

Step 1, find the file:

$ git log --name-only
fd8bc2a6 Merge branch 'XXX-1111-remove-search-queue' into 'main'
fd07ddc3 XXX-1111: Remove search queue

Step 2, restore the file:

$ git restore --source fd07ddc3~1 apps/cli/src/db/backfill-search-queue.command.ts

Note the ~1. The file in question was deleted in commit fd07ddc3, so to restore it we need to go one commit before it was deleted. ~1 does exactly that.

\- [ git ]