How To Recover Deleted Files in Git?

Last Updated: 2022-09-16
4.8
(19)
Summary: Git is a great way to track and control the development of any type of software project, but issues like accidentally deleted files can quickly make for significant headaches in your workflow. This article will describe the details involved in the deleted files in Git and touch on how you can do to recover deleted files in Git. 

What is Git?

Git is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all to be merged into one source.

So regardless of whether you write code that only you will see, or work as part of a team, Git will be useful for you:

  • Git is used to tracking changes in the source code.
  • The distributed version control tool is used for source code management.
  • It allows multiple developers to work together.
  • It supports non-linear development through its thousands of parallel branches.

Since the software is so important, data loss will be a bad thing for a dev. But fortunately, Git provides some commands for recovering data. Let’s learn them.

Methods to Recover Deleted Files in Git

Method 1: Recover Deleted Files in Git Using Git Command

Recovering a deleted file using the Git command line involves the git restore or git checkout command. Whenever you modify files in Git, including creating new files, editing, or deleting existing files, the changes start as unstaged. Then you stage the changes with the git add command, and finally, you commit the changes using the git commit command.

Git provides ways to recover a deleted file at any point in this life cycle of changes. Please run the following commands for different cases:

If you have not staged the deletion yet, simply run git restore <filename> and the file will be restored from the index.git restore-filename

If you have staged the changes, however, running git restore will throw an error, since the file does not exist in the index anymore. In this case, you need to run git restore –staged –worktree <filename>.  The –staged argument tells Git to restore the file in the index from HEAD, and the –worktree argument tells Git to restore the working tree as well.

git restore --staged --worktree -filename

If you have deleted the file and already committed the changes, you need to use the git checkout command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.

In the demo repo, file1.txt has already been deleted and committed. Let’s recover that file. To figure out which commit deleted file1.txt, you need to use the git rev-list command: git rev-list HEAD -n 1 — file1.txt.

git rev-list command

This command tells Git to list all commits, which can be reached from the HEAD, that changed the file file1.txt. The -n 1 option tells Git to limit the result to only one commit. The output is the checksum of the commit that deleted the file. You can check that this is the offending commit by using the git show command with the checksum.

The commit before this one is the last commit where this file was present. You can restore the file from that particular commit by running the following command. The ^ at the end of the commit hash tells Git to fetch the commit before this one:

git checkout 3d5210ddd8632d539ed3f5e362dc047ed508a510^ file1.txt

Method 2: Recover Deleted Files in Git Using Data Recovery Software

Besides restoring deleted files in Git, another effective and easy method is using professional recovery software to get back deleted files from Git. Bitwar Data Recovery Software is a professional data recovery tool supporting almost all formats in Windows and Mac.

This software recovers 580+ deleted, formatted, lost and disappeared data from internal & external hard drives, SD memory cards, USB flash drives, or other digital storage media on Windows or Mac. Any of the recognized data types are all supported, including documents, photos, video, audio, email and so on.

To recover deleted files from Git, just follow the 4 simple steps:

  1. Go to Bitwarsoft Download Center, find and install Bitwar Data Recovery on the computer.
  2. Select the Partitions or Devices where you lost files and then click Scan to scan.
  3. Choose the Quick Scan mode. You can choose the Deep Scan mode if the deleted data can’t be found. scan mode
  4. Choose your target file type, and click Scan.file types
  5. Choose and preview the deleted files, then click Recover and save the files into different devices or partitions.preview

NOTE: Remember to save all the deleted files to a different file location to prevent data from being overwritten!

Conclusion

This article introduces two methods to recover deleted files in Git: using commands in Git and data recovery software. If you like challenges, choose the first method. Otherwise, recommend using Bitwar Data Recovery Software to easily complete the recovery.

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 19

No votes so far! Be the first to rate this post.

Previous Article

5 Methods To Fix Mac Shuts Down Randomly Summary: Git is a great way to track and control the development of any type of software project, but issues...

Next Article

7 Methods To Fix Error Code 0xc00d5212 In Windows 10 Summary: Git is a great way to track and control the development of any type of software project, but issues...

About Bitwar Data Recovery

3 Steps to get back 500+ kinds of deleted, formatted or lost documents, photos, videos, audios, archive files from various data loss scenarios.

Learn More

Hot Articles

    Related Articles