Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
One of the most frequently discussed topics in job interviews and practical work is how to undo what's already been done. We'll examine three tools with different 'depths' of undoing and focus on what matters most: demonstrating specific scenarios where reset --hard can disrupt teamwork, while revert offers a safer alternative. We'll explore good and bad solutions with real-world consequences.
- You'll be able to confidently undo changes at any stage: in the working directory, in the staging area, and in the commit history.
- You'll understand the fundamental difference between reset and revert and know when each is appropriate to use.
- You'll learn to avoid common mistakes that beginners make when undoing changes in a team repository.
Phần đầu bài giảng
Git restore: The Undo Button for Files
- Git restore — undo uncommitted changes
- Works like an 'undo' button in an editor
- Safe — doesn't alter commit history
- Returns files to their last saved state
Imagine you've accidentally saved a file with errors and want to revert everything. Git restore acts like a magic undo button — it returns files to their last committed state. Unlike other undo commands, restore only works with uncommitted changes and is completely safe for your project's history. It's the first tool you should reach for when you need to quickly fix accidental edits without risking your change history.
Undoing Changes in the Working Directory
- git restore file.txt — return file to last commit
- Undoes all changes in working directory
- Safe — changes can be restored via editor
- Always check git status before restoring
Let's say you've spent half an hour editing a configuration file but realized you're going down the wrong path. Instead of manually reverting each change, execute git restore config.yml. The file will instantly return to its last committed state. It's like closing the file without saving, but with the guarantee that you're returning to a working version.
# Demonstrating file restoration
$ echo "Mistaken changes" > main.py
$ git status
$ git restore main.py
$ cat main.py
$ git status
Removing a File from the Index
- git restore --staged file.txt — remove file from the index
- Preserves changes in the working directory
- Warning: without --staged, changes will be lost
- Always check git status before executing
A common mistake is accidentally adding an unwanted file to the index. For example, you might have run 'git add .' and included a temporary settings file. The command 'git restore' with the --staged flag and the filename 'settings.tmp' will safely remove it from the index while preserving all changes in your working directory. Without the --staged flag, you would lose your work, so always check the status with 'git status' before running restore. Now you know how to undo changes before committing. But what if the mistake is already in the commit history? That's where git reset comes in.
# Accidentally added a file to the index
$ git add config.tmp
$ git status
# Remove from index while preserving changes
$ git restore --staged config.tmp
$ git status
# Verify the file remains in the working directory
$ ls -la config.tmp
Bài giảng đầy đủ dành cho thuê bao
Còn 24 slide nữa trong phiên bản đầy đủ.
Video, toàn bộ slide, ghi chú, bài kiểm tra, gia sư AI và phỏng vấn sẽ mở sau khi đăng nhập và đăng ký thuê bao.
Đăng nhập hoặc đăng ký Đăng ký thuê baoNội dung bài giảng
- Git restore: The Undo Button for Files
- Undoing Changes in the Working Directory
- Removing a File from the Index
- Git Reset: How Rewriting History Works
- Safe Reset --soft: Changes in the Index
- Standard reset --mixed: Changes in the Working Directory
- Dangerous reset --hard: Permanent Deletion
- Reset: Three Modes, One Command
- git reset --soft: Rewriting History Safely
- When to Use --mixed vs --hard
- Checklist: Which Reset to Choose?
- Why can't we just erase a mistake?
- How git revert works in practice
- Reverting multiple commits at once
- Reset: Rewriting Local History
- Revert: Safely Undoing Changes
- What Happens When You Push: Reset vs Revert
- Why reset --hard is dangerous
- Example of lost changes
- How to recover data
- The Dangers of Rewriting History in Shared Branches
- Safe Undo with Revert
- The Correct Workflow
- When to Use: restore, reset, or revert
- Rules for Working with Personal and Shared History
- Step-by-Step Guide for Fixing Mistakes
- Summary and Assignment: Master of Undo
Bài viết liên quan đến bài học này
- Lệnh Git: bảng tra cứu với ví dụ dùng hằng ngày
- git checkout, switch và restore: khi nào dùng cái nào
- git reset và git revert: cách hoàn tác commit mà không phá lịch sử nhóm
- git reflog: cách khôi phục commit và nhánh đã mất
- Git và GitHub: khác nhau thế nào và liên hệ ra sao
Tất cả bài giảng của khóa học
- Introduction: Git from Scratch to Confident Developer
- Why Git is Essential - And Why It's a Job Requirement
- Installation and Setup: Getting Git Ready for First Use
- First Repository: init, add, commit, and the Magic of the Index
- Reading History: log, diff, show and Navigating the Past
- Branches: The Parallel Reality of Your Code
- Undoing Mistakes: restore, reset, revert — Doing It Right and the Dangers
- GitHub and Remote Repositories: Your Code in the Cloud
- Team Collaboration: Forks, Feature Branches, and Pull Requests
- Merge Conflicts: Don't Panic - Understanding and Resolving Them
- Git Workflows in Real Teams: Choosing the Right Approach
- Advanced Tools: Rebase, Stash, Cherry-pick and Tags
- Final Project: Simulating Real Team Development