How to restore a deleted file from a Git repository

In a previous commit, you deleted a file that you now need back. How do you restore it?

The git restore command allows you to restore a deleted file from a Git repository. However, you need the commit ID of the commit from which you want to restore the file.

Let’s say you are in a clean repository.

alina

alina git:( main ) ls

README.md file1.txt

alina git:( main )

The git status command confirms that the repository is clean.

alina

alina git:( main ) git status

On branch main

Your branch is ahead of 'origin/main' by 5 commits.

  (use "git push" to publish your local commits)

nothing to commit, working tree clean

alina git:( main )

You know that in the past, you had a file named file2.txt in the repository and you want to restore it.

The git rev-list command allows you to list the commits that modified a file.

alina

alina git:( main ) git rev-list HEAD -- file2.txt

44efd6005ab1cc8d7d87aefd75d05447a6fdfe6a

cd143755fd7f16d1804b27beb3a24b63cd7df627

634a01169a3b751003a33715a5983e6f1a514671

alina git:( main )

The output shows that 3 commits modified the file. By definition, the last commit is the one that deleted the file.

alina

alina git:( main ) git show 44efd6005ab1cc8d7d87aefd75d05447a6fdfe6a

commit 44efd6005ab1cc8d7d87aefd75d05447a6fdfe6a

Author: alina <alina@email.com>

Date:   Tue Jun 27 08:10:08 2023 -0700

    delete file2.txt and update file1.txt

diff --git a/file1.txt b/file1.txt

index 35d242b..3cc58df 100644

--- a/file1.txt

+++ b/file1.txt

@@ -1,2 +1 @@

-A

-B

:

alina git:( main )

The comment of the commit confirms that: delete file2.txt and update file1.txt, but that’s anecdotal.

You have 2 options to pick the commit ID:

  • Either you pick the commit before the last commit, which is the last commit that changed the file you want to restore.
  • Or you pick the parent of the last commit. Since the last commit deleted the file, the parent of that commit still has the file.

Let’s go with option 1 and use the second commit in the list:

alina

alina git:( main ) git restore --source cd143755fd7f16d1804b27beb3a24b63cd7df627 -- file2.txt

alina git:( main )

The file is back:

alina

alina git:( main ) ls

README.md file1.txt file2.txt

alina git:( main )

And you can check its content:

alina

alina git:( main ) cat file2.txt

1

2

alina git:( main )

The git restore command only updated the working directory, and the file is not staged.

alina

alina git:( main ) git status

On branch main

Your branch is ahead of 'origin/main' by 5 commits.

  (use "git push" to publish your local commits)

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        file2.txt

nothing added to commit but untracked files present (use "git add" to track)

alina git:( main )

You can now stage it.

alina

alina git:( main ) git add file2.txt

alina git:( main )

And commit it:

alina

alina git:( main ) git commit -m 'add again file2.txt'

[main 543887b] add again file2.txt

 1 file changed, 2 insertions(+)

 create mode 100644 file2.txt

alina git:( main )