snippets/old_snippets/recover-deleted-file-git.en.md

909 B

title seo_description date draft snippet_types
how to recover deleted file with git short git tutorial on how to recover deleted files with git 2023-09-12T09:30:17+02:00 false
git

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
/command
fd8bc2a6 Merge branch 'XXX-1111-remove-search-queue' into 'main'
fd07ddc3 XXX-1111: Remove search queue
apps/cli/src/cli.module.ts
apps/cli/src/db/backfill-search-queue.command.ts
apps/cli/src/db/db.module.ts
...

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.