How To Recover Deleted Files in Git?

Останнє оновлення: 2022-09-16
4.8
(19)
Резюме: 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 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. Перейти до Центр завантажень Bitwarsoft, знайдіть і встановіть Відновлення даних Bitwar на комп'ютері.
  2. Виберіть Розділи або пристрої where you lost files and then click Scan to scan.
  3. Виберіть Швидке сканування режим. Ви можете вибрати Глибоке сканування mode if the deleted data can’t be found. scan mode
  4. Виберіть цільовий тип файлу та натисніть Сканувати.file types
  5. Виберіть і попередньо перегляньте видалені файли, а потім натисніть Відновити і збережіть файли на різних пристроях або розділах.preview

ПРИМІТКА: не забудьте зберегти всі видалені файли в іншому місці, щоб запобігти перезапису даних!

Висновок

This article introduces two methods to recover deleted files in Git: using commands in Git and програмне забезпечення для відновлення даних. If you like challenges, choose the first method. Otherwise, recommend using Програма для відновлення даних Bitwar to easily complete the recovery.

Наскільки корисним був цей пост?

Натисніть на зірочку, щоб оцінити!

Середня оцінка 4.8 / 5. Підрахунок голосів: 19

Голосів поки немає! Будьте першим, хто оцінить цю публікацію.

Попередня стаття

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...

Наступна стаття

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...

Про Bitwar Data Recovery

3 кроки для повернення понад 500 видів видалених, відформатованих або втрачених документів, фотографій, відео, аудіо, архівних файлів із різних сценаріїв втрати даних.

Дізнайтеся більше

Гарячі статті

    Схожі статті