📖 Scenario: Imagine you are working on a project with Git. Sometimes, mistakes happen like deleting files or committing wrong changes. Knowing how to recover from these mistakes quickly is very important to keep your work safe and avoid losing progress.
🎯 Goal: You will learn basic Git recovery commands to undo mistakes and restore your project to a good state.
📋 What You'll Learn
Create a Git repository with a file
Make a commit with the file
Simulate a mistake by deleting the file
Use Git commands to recover the deleted file
Display the recovered file content
💡 Why This Matters
🌍 Real World
In real projects, mistakes like deleting files or committing wrong changes happen often. Knowing how to recover quickly saves time and prevents data loss.
💼 Career
Git recovery skills are essential for developers, DevOps engineers, and anyone working with version control to maintain code integrity and fix errors efficiently.
Progress0 / 4 steps
1
Create a Git repository and add a file
Initialize a Git repository with git init. Create a file named notes.txt with the content Hello Git. Add the file to Git with git add notes.txt and commit it with the message Initial commit using git commit -m "Initial commit".
Git
Hint
Use git init to start a repo. Use echo to create the file. Then add and commit.
2
Simulate a mistake by deleting the file
Delete the file notes.txt using rm notes.txt. Check the Git status with git status to see the file is deleted but not committed yet.
Git
Hint
Use rm to delete the file. Then run git status to see the change.
3
Recover the deleted file using Git
Use git checkout -- notes.txt to restore the deleted file from the last commit. This recovers your file without losing any work.
Git
Hint
Use git checkout -- filename to restore a deleted file from the last commit.
4
Display the recovered file content
Use cat notes.txt to display the content of the recovered file and confirm it says Hello Git.
Git
Hint
Use cat filename to see the file content in the terminal.
Practice
(1/5)
1. Why are recovery skills important when using git?
easy
A. They speed up the commit process
B. They help fix mistakes without losing work
C. They automatically merge branches
D. They delete old branches permanently
Solution
Step 1: Understand the purpose of recovery skills
Recovery skills allow you to undo or fix errors safely in your code history.
Step 2: Identify the benefit of recovery skills
They prevent loss of work by enabling you to restore previous states or undo changes.
Final Answer:
They help fix mistakes without losing work -> Option B
Quick Check:
Recovery skills = Fix mistakes safely [OK]
Hint: Recovery means fixing errors without losing code [OK]
Common Mistakes:
Thinking recovery speeds commits
Confusing recovery with merging
Believing recovery deletes branches
2. Which git command is used to view the history of changes including those not on any branch?
easy
A. git revert
B. git reset
C. git reflog
D. git status
Solution
Step 1: Identify command purpose
git reflog shows a log of all changes to HEAD, including commits that are no longer referenced by branches.
Step 2: Compare with other commands
git reset changes HEAD, git revert creates new commits to undo changes, and git status shows current state.
Final Answer:
git reflog -> Option C
Quick Check:
History including lost commits = git reflog [OK]
Hint: Use reflog to see all HEAD changes, even lost commits [OK]
Common Mistakes:
Using git reset to view history
Confusing git revert with reflog
Thinking git status shows past commits
3. What is the output of the following commands sequence?