Restore a Deleted File

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a popular version control system that allows developers to track changes made to their code over time. One of the benefits of using Git is that it allows you to restore deleted files. In this lab, you will learn how to restore a file that was deleted in a specific commit.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/DataManagementGroup -.-> git/restore("`Revert Files`") subgraph Lab Skills git/restore -.-> lab-12754{{"`Restore a Deleted File`"}} end

Restore a Deleted File

You are working on a project using Git and accidentally deleted a file named file2.txt that you need. Fortunately, you know the commit where the file was deleted. Your task is to restore the deleted file using Git.

To complete this lab, you will use the Git repository git-playground from https://github.com/labex-labs/git-playground.git. Follow the steps below:

  1. Navigate to the repository directory using the command cd git-playground.
  2. Run the command git log --oneline to view the commit history.
  3. Identifies a commit where a file was deleted with the message "Added file2.txt".
  4. Run the command git checkout <commit> -- <file> to restore the specified <file> deleted in the specified <commit>. Replace <commit> with the commit hash and <file> with the name of the deleted file.

For example, if the file file2.txt was deleted in the commit d22f46b, you would run the following command:

git checkout d22f46b -- file2.txt

This will restore the file2.txt file to your local repository.

This is the result of running the ll command:

total 12K
-rw-r--r-- 1 labex labex 15 Jun 18 18:05 file1.txt
-rw-r--r-- 1 labex labex 15 Jun 18 18:13 file2.txt
-rw-r--r-- 1 labex labex 32 Jun 18 18:05 README.md

Summary

In this lab, you learned how to restore a deleted file using Git. By using the git checkout command with the commit hash and file name, you can easily restore a deleted file to your local repository.

Other Git Tutorials you may like