How to Recover Deleted Files from a Local Repository in Git

How to Recover Deleted Files from a Local Repository in Git

Deleted your code or a file in error on Git? Don't worry. You can recover your files using this guide

Wondershare Recoverit Authors

Feb 02, 2021 • Filed to: Recover & Repair Files • Proven solutions

0

basic git interface

Asides debugging code for long hours and staring at screens for long hours, one thing developers hate is losing their source codes. As a dev, you must have deleted your code in error one time or the other. It is possible to recover those files on Git. In very easy and simple to describe steps, we’ll look over peculiar file losses and how to recover them on Git. While we’re at it, you’ll learn what Git is, what makes it special, and why you need one as a dev. Let’s go, shall we?

What is Git?

git logo

Git is simply storage for code files present on each developer’s system. It serves as a content tracker for code with a ‘version control system’. This VCS allows developers to make changes to the code and notes those changes in a history format. Now the great part of Git is that there is a local repository and the remote repository. The local repo is found on each developer’s system (as stated earlier). The remote repo is hosted on a central server that allows copies of the files to be available on each dev’s computer. Great right?

Git was created because, in reality, a lot of developers work on single projects and so serves as a storage for these code files. This allows as many people as possible to work on the same code file regardless of where they are in the world. Different scenarios may come up with deleting files on Git. We have detailed 4 of those cases and 5 solutions to recover lost files from your local repository on Git.


Case 1: I deleted a file but I did not commit

Losing your files on git after committing your work on Git is very rare. In fact, it takes deleting your entire local repository and not having any backup on the remote server to totally lose a file on Git.

If you deleted a file without committing, and immediately found your error? Simply enter the code as seen below

deleted file but did not commit


Case 2: I deleted a file on Git and committed

This is for when you deleted a file, committed the deletion, and found out you needed the file. To recover this file, you need to do perform a reset. This reset will return you to a state before your commit. You should take care before running the command because the ‘Hard’ instruction on the code will delete other changes made to the file after the commit.

deleted file and committed

This method acts on the assumption that you haven’t sent your file to a remote server. If you have, see case 4 below.


Case 3: I committed a deletion and then I made other commits

Working on a project, committed a deletion, and then went ahead to work, making more commits while at it? Your file is still recoverable. You need to look for the commit that still has the deleted file, as below.

git log for commit history

When the results come, you have 2 options to recover your file.

1. You can choose to work with the commit that still has the file. You have to check out the file from that commit as below

check out the file from the commit

2. You can also choose to work with the commit that deleted the file. You have to check out the file from one commit before that as seen below

check out the file before the commit


Case 4: I deleted a file, committed and sent to a remote server

This seems final and fatal. However, your files can be recovered. If you are thinking of resetting and pushing the file again, you’re wrong! There may be damages done to the file as the local repository’s history will be rewritten. So, what do we do?

We perform a new commit that tries to undo the function of the commit that deleted the file. As below:

the deleted file, committed and pushed

The "--no-commit" command above stops the instruction from creating a fresh commit immediately. Thus, allowing you to select the specific changes in the old commit you wish to alter in your new commit. The <commit> is the exact commit responsible for deleting the file.


Try to recover the files from the drive directly

This is a viable method to recover the files because even if the files are deleted from the computer, data still remains on a sector of the drive. You can use a good data recovery software to scan the particular drive to find the deleted files. A perfect application for this is Wondershare Recoverit. With this software, you will recover your lost and deleted files in less than 5 minutes.

Step 1. Download Wondershare Recoverit on your Mac or Windows computer. Install on your system and then open the application

Step 2. Ensure that the drive is connected to the PC and then select it as the location to start recovering files. Click on Start after selecting the given drive.

select location to be scanned

Step 3. The application scans and checks the drive for the files as you can see below

recoverit scans the system for lost files

Step 4. Once the search is complete, you should be able to see the files and select the ones you wish to recover. Once you’re done selecting, click on the “Recover” button on the bottom right corner of the screen and just like that, your files are recovered to their original location

select files and recover


Bottom Line

Before you perform all other Git data recovery methods, you should try to recover directly from the drive using Recoverit. Recoverit is very smooth and works seamlessly to get you your lost files in no time. Other methods work but you should take care to not send erratic commands and cause further damage. Wishing you all the best on your data recovery journey!

Recoverit author

Eleanor Reed

staff Editor

(Click to rate this post)

Generally rated4.5(105participated)

Home > Resources > Recover & Repair Files > How to Recover Deleted Files from a Local Repository in Git
ubackit v1.5