Recover deleted files in Git [With example commands]

In this tutorial, I’ll be explaining how to recover deleted files in Git. Let’s assume a few scenarios and see how Git file recovering commands are working.

Git Version

All the commands in this page were executed in git version 2.20.1.windows.1

I deleted a file but not staged for commit

Ok, you realized your mistake as soon as you deleted the file, then it’s quite easy to recover the file to its original form. Assume that you deleted tutorials.html file and executing git status says that the changes are not staged for commit, but the file has been deleted.

> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: tutorials.html

no changes added to commit (use "git add" and/or "git commit -a")

The above output says that the file has not been staged, then you can simply execute the below command to recover the deleted file.

> git checkout HEAD tutorials.html

The above command will work whether you had staged the file for commit or not.

I deleted the file, staged for commit but did not commit

Assume that you had deleted the file mistakenly and staged it for commit.

> git add -A

But you realized your mistake before performing the commit. Here, the file is in staging area and it can be viewed using git status command.

> git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted: tutorials.html

Now executing the below command will recover the deleted file to its original form, even though it was staged for commit.

git checkout HEAD tutorials.html
Note:

Remember, if you had made some changes to the file, saved and then deleted by mistake. Then the files recovered using the above commands will not have the changes made locally. It will only recover the file to its original form.

Alternatively, the output of the git status command tells us that the file can be unstaged as shown below:

> git reset HEAD tutorials.html
Unstaged changes after reset:
D tutorials.html
Git Version - 2.20.1.windows.1

The above command will un-stage the delete operation. But wait, the file is not recovered yet and you will not see that in the file explorer or in the visual studio. Now executing git status will give you command to recover the file to its original form.

> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: tutorials.html

no changes added to commit (use "git add" and/or "git commit -a")

Executing the below command will recover the file.

git checkout -- tutorials.html

Here, means current branch.

Deleted files and want to recover that?

Assume that you had deleted two files tutorials.html and README.md and you haven’t committed yet, then executing git status would display the deleted files in red.

> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: README.md
deleted: tutorials.html

no changes added to commit (use "git add" and/or "git commit -a")

Now executing the below command will display all the unstaged changes after RESET.

> git reset HEAD tutorials.html README.md
Unstaged changes after reset:
D README.md
D tutorials.html

You can now recover the deleted files using the below command:

git checkout -- tutorials.html README.md

Deleted many files and want to recover that?

What if you had deleted many files and wanted to recover those? Of course, you can execute git reset <file1> <file2> <file n> and  git checkout -- <file1> <file2> <file n>. But that would definitely be a cumbersome task to do. In such cases, you can recover multiple deleted files using a wildcard (*).

git reset HEAD * 
Unstaged changes after reset:
D README.md
D tutorials.html

The above command will un-stage all the deleted files. Now executing git checkout -- * would easily recover all the deleted files.

git checkout -- *

I deleted the file and committed the deletion, but not pushed

Ah, you deleted the file and committed as well and now you realized that you wanted that file back? In this case, you have to go back to the state before your commit was made.

> git reset --hard HEAD~1
Caution:

–hard option will discard all changes made to the tracked files after the specified commit.

I deleted the file and committed multiple times

Assume that you had deleted a file, committed the change, continued to work on other files, and did few more commits. Now you realized that you wanted the file that you deleted a few commits before? Well, it’s still possible to recover. To do that, you need to list the log of the deleted file using the below command:

> git log -- README.md
Author: test<58256027+test@users.noreply.github.com>
Date: Fri Jun 26 15:45:13 2020 +0530

removed duplicate codes

commit 379b549b7afb76cdbc77887d4a234e80e6599c54
Author: test<58256027+test@users.noreply.github.com>
Date: Fri Jun 26 11:32:43 2020 +0530

Changed the Readme

commit 623a76e681d99113981d1f823de662bbf257c3df
Author: author<author1>
Date: Tue May 26 15:14:13 2020 -0400

remove stray quotation

commit 93012e194cd2fe0f3858b73fd1cb9fe99b38c88a

The above output displays the most recent commit at the top. So you can now decide which commit you to want to recover the deleted file. All you need is to copy the commit hash and use it in the below command.

> git checkout 93012e194cd2fe0f3858b73fd1cb9fe99b38c88a -- README.md

In case, if you had deleted the file, committed and pushed to the remote, then recovering the file and pushing it again might cause a lot of problems. Anyways, this part is covered in git-tower.

Well, that’s it for now and I’ll update this article with more commands. See you until then.

Updated on June 26, 2020

Was this article helpful?

Related Articles

Leave a Comment