Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
Git is a powerful version control system that allows developers to track changes to their codebase. However, sometimes files can become lost or accidentally deleted. In this lab, you will learn how to use Git to find lost files and commits.
Find Lost Files
You have been working on a project in the git-playground repository. However, you have noticed that some files are missing and you are not sure when they were deleted or how to recover them. Your task is to use Git to find any lost files and commits in the repository.
- Clone the
git-playgroundrepository:
git clone https://github.com/labex-labs/git-playground.git
- Navigate to the directory and configure the identity:
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
- Create and switch a branch named
one-branch, deletefile2.txtand commit with the message "Remove file2":
git checkout -b one-branch
git rm file2.txt
git commit -m "Remove file2"
- Switch back to the
masterbranch and delete theone-branchbranch:
git checkout master
git branch -D one-branch
- Run the
git fsck --lost-foundcommand to find any lost files and commits:
git fsck --lost-found
- Check the
.git/lost-founddirectory to see if any lost files were recovered:
ls .git/lost-found
- If any lost files were found, review them to determine if they are the missing files.
This is the result of running the ls .git/lost-found command:
commit
Summary
Using Git to find lost files and commits can be a lifesaver when working on a project. By running the git fsck --lost-found command, you can identify any dangling objects and extract them into the .git/lost-found directory. From there, you can review the files to determine if they are the missing files.