Recover the Lost Files

GitGitBeginner
Practice Now

Introduction

As a developer, you might encounter situations where important files are accidentally deleted and committed. This challenge simulates such a scenario, testing your ability to use Git's history inspection and state restoration tools to recover lost files and restore your project to a working state.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BranchManagementGroup -.-> git/checkout("Switch Branches") git/BranchManagementGroup -.-> git/log("Show Commits") git/BranchManagementGroup -.-> git/reflog("Log Ref Changes") subgraph Lab Skills git/add -.-> lab-387781{{"Recover the Lost Files"}} git/status -.-> lab-387781{{"Recover the Lost Files"}} git/commit -.-> lab-387781{{"Recover the Lost Files"}} git/checkout -.-> lab-387781{{"Recover the Lost Files"}} git/log -.-> lab-387781{{"Recover the Lost Files"}} git/reflog -.-> lab-387781{{"Recover the Lost Files"}} end

Recover the Lost Files

Before starting this challenge, a Git repository has been initialized for you in the ~/project/important-project directory. This repository contains several commits, including the creation and subsequent accidental deletion of two important files. Here's what you need to know:

  • The repository was created with an initial commit adding a README.md file.
  • Two crucial files, important_algorithm.py and crucial_data.txt, were added in a subsequent commit.
  • There were a few more commits with various changes.
  • At some point, important_algorithm.py and crucial_data.txt were accidentally deleted, and this deletion was committed.
  • There has been at least one more commit after the accidental deletion.

Your task is to recover the deleted files by navigating the repository's history and then commit the recovered files.

Tasks

  1. Investigate the repository's history to find when important_algorithm.py and crucial_data.txt were deleted.
  2. Restore the repository to the state just before the deletion.
  3. Keep the original commits and create a new commit with the message "Recover deleted files".

Requirements

  • Work in the ~/project/important-project directory.
  • Ensure important_algorithm.py and crucial_data.txt are present after recovery.
  • Make sure the last commit message is "Recover deleted files", and the commit history is preserved.

Example

After successfully completing the challenge, running ls in the project directory should show:

$ ls ~/project/important-project
important_algorithm.py crucial_data.txt README.md

Running git status should show a clean working directory:

$ git status
On branch master
nothing to commit, working tree clean

And git log should show your new commit at the top:

$ git log --oneline
8876d8b (HEAD -> master) Recover deleted files
9b3525c Update project status
5e6234c Oops, accidentally deleted important files
9de9506 Update algorithm TODO
01c36ae Add crucial data and algorithm
638a98d Initial commit
โœจ Check Solution and Practice

Summary

In this challenge, you faced a common Git scenario: accidentally deleted files. You learned how to:

  1. Inspect your repository's complete history, including actions that changed the HEAD.
  2. Restore your repository to a specific previous state, effectively "turning back time" in your project.
  3. Create a new commit to save the restored state of your project.

These skills are invaluable for finding lost commits, undoing mistakes, and maintaining a clear project history. Remember, while these tools can save you from disaster, it's always best to work carefully and make frequent, well-documented commits to avoid such situations in the first place.

By completing this challenge, you've gained practical experience with some of Git's most powerful recovery tools, skills that will serve you well throughout your development career.