After working long hours writing code, fixing bugs, and staring at the computer screen, one thing programmers hate the most is accidentally deleting or losing their source codes. If you’ve been programming for a while, you must have deleted your codes or code files accidentally at least once. Ah! Let’s not talk about the urge you felt to smash the computer into pieces when you mistakenly erased either of the two. But don’t worry, it’s possible to recover .git files you have deleted using Git or data recovery software.
In this article, we will show you how to restore your deleted code files using Git in multiple scenarios. We will also describe what Git is and why every developer should have it on their computer to make their life easier. In the end, we will also showcase using professional data restoration software to retrieve your files.
Let’s dive in.
What is Git?
Git, in simple words, is storage for codes with a “version control system” or VCS that is used by almost all the developers in this world. Git allows its users to make changes in their codes and make a note of updates. One of the greatest features of Git is the presence of two separate repositories: local repository and remote repository. The local repository exists in every programmer’s computer’s local storage. In contrast, the remote repository is a wide network of servers that allows every developer to get access to the code stored there.
The truth is that many programmers work on similar projects. Git works as a storage for the codes of those similar projects. This allows multiple programmers all over the world to see the code file and make suitable changes to them in their local repository to fit their needs. As a result, this saves the developers a lot of time as they don’t need to start many projects from scratch and gain better insights and inspirations through it.
But when you delete those files accidentally, a lot of different scenarios may come up, making the restoration process way too complicated.
How to Recover .git Files Using Git Downloaded to Your Computer
In this section, we will show you how to restore the deleted .git files using the Git application downloaded to your computer.
Situation 1: Recovering a File You Have Deleted but Didn’t Commit
It’s quite rare to lose your files in Git after you delete a file but don’t commit the changes. After that, to lose a file completely in Git, you need to delete an entire repository and make sure there is no backup of the file in any remote server. Therefore, it’s pretty easy to recover .git files if you just deleted them and have yet to commit the differences.
In this case, open Git on your computer and try typing this command and executing it: $ git checkout HEAD <filename>
Replace the “filename” part with the actual file name of the .git file you have just deleted. After you execute the command we have mentioned above, you can recover all .git files you have erased but haven’t committed to the changes yet.
Situation 2: Recover .git Files You Have Deleted on Git and Committed
Did you delete a .git file and already commit the altered state? In this case, the command line we have mentioned in situation 1 isn’t sufficient for recovery. To restore a removed file you have already committed, you need to do a reset in Git. The action of resetting Git will revert the state of the deleted back to when you didn’t submit its deletion yet.
Use this command and execute it to reset the Git to a state before you had committed to the deletion of your file: $ git reset --hard HEAD~1
Now, the file is back to a state when you removed the file from Git but didn’t commit it yet. Thus, you can use the command line mentioned in situation 1 and recover .git files.
Please note that this method is viable only when you are yet to send the file to a remote server. If you have already sent the file to a remote server, you need to use the command mentioned in situation 4 to initiate its restoration.
Situation 3: Restore A File Whose Deletion You Committed and Made a Few More Commits After That
Have you removed a file from Git, committed the action, made a few more changes on a project, and submitted several commits to the newly made changes? Despite being late to the party in realizing you want the file you have removed back, don’t lose hope.
Although you did make multiple commits after the deletion of the file, you can still recover it. Here’s how you can do it:
- First, you need to find the commit that still has the file you have deleted. To do it, type this command line on Git and execute it:
$ git log -- <filename>
- Once you execute the command mentioned above, you have two ways to restore your erased .git file.
- Your first option is to work with the commit that still has the file and recover your file from it. To do that, you need to check out the file from the chosen commit by executing this command in Git:
$ git checkout <commit hash> -- <filename>
- The second avenue to explore is working with the commit that actually deleted your files and recover .git files from it. Once you find the commit that erased your file, you just need to check out the file from the previous commit, i.e., the commit before the commit that deleted your files. To check out and rescue your file, execute this command in Git:
$ git checkout <deletion commit hash>~1 -- <filename>
- Your first option is to work with the commit that still has the file and recover your file from it. To do that, you need to check out the file from the chosen commit by executing this command in Git:
You can retrieve your deleted .git file using any of the two options mentioned above. Again, make sure to substitute the “filename” portion of the commands with the exact name of the missing .git file.
Situation 4: Retrieve .git Files You Have Deleted, Committed, and Sent to a Remote Server
This is the most severe situation, but not because it’s more difficult to recover the file. Instead, it’s since most people make serious mistakes while retrieving their deleted files at this point. When faced with this situation, many people think about resetting Git to a state before the commit and recovering the file using the commands from situation 2.
That’s the wrong choice! Once you delete your file, commit it, and send it to a remote server, resetting Git to the state before committing might damage your file instead and rewrite the local repository’s file history.
Therefore, to get your deleted file back, you need to make a new commit that will undo the previous one (the one that removed the file you seek to rescue). To do that, type this command in Git and execute it: $ git revert --no-commit <commit>
The “--no-commit”
portion of the command will stop Git from creating a new commit. This will help you make any changes in an old commit. On the other hand, the “<commit>”
part is the commit that has deleted the file you’re looking for.
How to Recover .git Files Directly from the Disk Drive Using a Data Recovery Application
Even if your .git files have been deleted from your computer, they will still exist in a particular sector of your computer’s disk drive. Therefore, if you’re looking for a simple method to restore deleted .git files without straining your head with complicated command lines, using professional data recovery software is the way to go. Since most reputable data recovery applications have similar UI and recovery processes, the instructions are universal. Follow the steps below to get your .git files back:
- Download and install a professional data recovery software and/or application, depending on your operating system.
Note. Double-check it’s the official software from a renowned company. - Launch the program on your computer.
- Select the drive where your deleted .git files were located, for instance, the “C:” drive.
- Once you pick the correct drive, click the “Scan” option. The application will show you the deleted files from the selected drive once the scan is complete.
- Next, look for the deleted files and once you find them, select them (usually through a checkmark list).
- Click on the “Recover” option and your removed files will be saved to their original location.