Git Reset, Restore, and Revert
# CHAPTER 8
Git Reset, Restore, and Revert
1. Introduction
Every developer types bad code, commits bugs, and occasionally ruins a perfectly good file. Version control exists specifically to undo these mistakes. However, Git provides three distinct commands for time travel:reset, restore, and revert. Beginners often confuse them, deploying the wrong command and inadvertently causing more destruction. In this chapter, we will definitively untangle this trio of commands. We will explore the architectural differences between modifying the working directory, altering the staging area, and rewriting the commit graph, ensuring you select the mathematically correct tool for the specific emergency.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use
git restoreto unstage files and discard local, uncommitted changes.
-
Differentiate between
git reset --soft,--mixed, and--hard.
-
Understand how
git resetfundamentally rewrites the branch pointer.
-
Use
git revertto safely undo a commit without rewriting history.
- Select the correct undo command based on whether the mistake is public or private.
3. Beginner-to-Advanced Explanations
The Scope of Undo: Git tracks files in three locations: the Working Directory (your hard drive), the Staging Area (theadd zone), and the Commit History (the database).
-
git restore: Modifies the Working Directory and Staging Area. It says: "Put this specific file back exactly how it looked yesterday." It does NOT touch the commit graph.
-
git reset: Modifies the Commit History (and optionally the other two). It says: "Take the entire branch pointer and rip it backward in time." It erases commits from existence.
-
git revert: Modifies the Commit History safely. It says: "Read the bad commit, figure out the mathematical inverse of the code, and create a brand *new* commit moving forward that cancels out the mistake."
4. Git Command Walkthroughs
The Safe Undo: git restore
You are typing in app.js. You delete 50 lines of code by mistake and save the file. You haven't committed yet.
The Destructive Time Machine: git reset
You committed three terrible, broken commits locally. You want them gone forever.
The Public Apology: git revert
You committed a bug and pushed it to GitHub. Your team downloaded it. You CANNOT use reset because erasing public history breaks your coworkers' laptops.
5. Mini Project: Recover Accidentally Deleted Commits
Let's simulate the panic of a hard reset and recover it.Step-by-Step Walkthrough:
-
1.
Initialize:
mkdir reset-demo && cd reset-demo && git init
-
2.
Create base:
echo "V1" > file.txt && git add . && git commit -m "V1"
-
3.
Create the diamond:
echo "V2 Perfect Code" > file.txt && git commit -am "V2"
-
4.
The Mistake: You meant to type
--soft, but you accidentally typed:
bash
git reset --hard HEAD~1
`
-
5.
Check your folder.
file.txt says "V1". Your perfect V2 code is completely eradicated from your hard drive and the git log.
-
6.
The Rescue: As learned previously, the Git Reflog is tracking the branch tip.
`bash
git reflog
`
-
7.
Find the hash of the "V2" commit (e.g.,
a1b2c3d).
-
8.
Force the branch pointer *forward* in time back to the deleted hash!
`bash
git reset --hard a1b2c3d
`
-
9.
Check your folder. The "V2 Perfect Code" is resurrected!
6. The Decision Matrix
Print this out:
-
Mistake is UNCOMMITTED: Use
git restore.
-
Mistake is COMMITTED but LOCAL: Use
git reset.
-
Mistake is COMMITTED and PUSHED to GitHub: Use
git revert.
7. Best Practices
-
The Power of Soft Reset:
git reset --soft is an incredible tool for squashing commits without dealing with interactive rebasing. If you have 5 messy commits, you can simply git reset --soft HEAD~5. All your code changes are kept perfectly intact in the staging area. You just type one git commit -m "All 5 features combined" and you have instantly squashed them.
8. Common Mistakes
-
Confusing Reset with Revert: Junior developers constantly use
git reset --hard to undo a commit they already pushed to GitHub. When they try to git push again, GitHub throws a massive error rejecting the push, because the local laptop is now "behind" the cloud server. They then panic and type git push --force, destroying the history for the entire company.
9. Troubleshooting Tips
-
Detached HEAD State: If you try to checkout a specific old commit hash instead of a branch name (
git checkout 9f8a7b6), Git puts you in a "Detached HEAD" state. You are looking at the past, but you are not on a branch. Any commits you make here will float in the void and be deleted by garbage collection. To fix this, if you want to keep working in the past, instantly create a branch: git checkout -b new-branch-from-past.
10. Exercises
-
1.
According to the Decision Matrix, what is the mathematically correct command to undo a commit that has already been pushed to a shared production repository?
-
2.
Explain the physical difference in the working directory when executing a
--hard reset versus a --soft reset.
11. FAQs
Q: I used git restore to delete an uncommitted file change, but now I want that change back. Can I use the reflog?
A: No. The reflog *only* tracks commits. If you type code, never commit it, and then use git restore (or checkout) to discard those changes, the code is permanently deleted from your hard drive and cannot be recovered by Git.
12. Summary
In Chapter 8, we definitively categorized Git's time-travel capabilities. We established git restore as the surgical tool for local, uncommitted file manipulation. We dissected the immense, history-altering power of git reset, delineating the critical difference between the destructive --hard flag and the code-preserving --soft flag. Finally, we cemented the necessity of git revert as the only acceptable mechanism for undoing mistakes that have already contaminated public, shared repositories, ensuring our fixes never fracture the team's historical graph.
13. Next Chapter Recommendation
We have referenced the reflog` multiple times as a magical safety net. It is time to fully dissect how this hidden ledger actually operates. Proceed to Chapter 9: Git Reflog and Recovery Techniques.