MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Undoing Changes Safely: git restore, git reset, and git revert0%
Study Materials
Git & GitHub BasicsProgress 0%
1.1What is Version Control? (Local, Centralized vs Distributed VCS)1.2Why Git? History, Architecture & Core Benefits1.3The Three Trees: Working Directory, Staging Area & Git Repository1.4Installing Git and Initial Configuration (git config user.name / email)
7.1Shelving Changes Temporarily with git stash (save, pop, apply, drop)7.2Undoing Changes Safely: git restore, git reset, and git revert7.3Rebasing Branches: Linear History with git rebase vs git merge7.4Tagging Milestones and Software Releases with git tag
HomeTutorialsGit & GitHub BasicsUndoing Changes Safely: git restore, git reset, and git revert
Rebasing Branches: Linear History with git rebase vs git merge

Undoing Changes Safely: git restore, git reset, and git revert

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Making mistakes is an inevitable part of programming. Git's greatest strength is its ability to undo almost any action. However, because different undo commands have vastly different consequences, understanding when to use git restore, git reset, or git revert is critical to avoiding accidental data loss.


1. Decision Matrix: Which Undo Command Should You Use?

1What do you want to undo?
2|
3+---> 1. Unstaged changes in Working Directory? ---------> `git restore <file>`
4|
5+---> 2. Staged changes in Index? -----------------------> `git restore --staged <file>`
6|
7+---> 3. Local unpushed commit(s) on your machine? ------> `git reset` (soft / mixed / hard)
8|
9+---> 4. Commit already pushed to a shared remote repo? -> `git revert <commit-hash>`

2. Modern Undoing in the Working Tree: git restore

Introduced in Git 2.23, git restore is designed for safety:

1# 1. Discard uncommitted changes in a specific file (IRREVERSIBLE!)
2git restore index.html
3 
4# 2. Discard all uncommitted changes across entire working tree
5git restore .
6 
7# 3. Unstage a file (moves it out of Staging Area back to modified in working tree)
8git restore --staged package.json

3. Rewinding Local History: git reset

git reset moves the current branch pointer backward in time. It has three distinct modes:

1 (C1) <--- (C2) <--- (C3) [HEAD, main]
2 ^
3 |
4 Command: git reset HEAD~1

The Three Modes of git reset:

ModeMoves Branch Pointer Back?Keeps Changes in Staging Area?Keeps Changes in Working Directory?Safety Level
--softYesYes (Staged)YesCompletely Safe
--mixed (default)YesNo (Unstaged)YesSafe
--hardYesNoNo (Destroyed!)DANGEROUS!

Practical Examples:

1# Undo the last commit, but KEEP all modified files staged in index:
2git reset --soft HEAD~1
3 
4# Undo the last commit and unstage files, but keep edits on disk in working tree:
5git reset HEAD~1
6 
7# Completely obliterate the last commit and wipe out all local file modifications:
8git reset --hard HEAD~1
Important Warning
[!CAUTION] git reset --hard permanently discards uncommitted work in your working tree. There is no undo for uncommitted files lost to --hard!

4. The Collaborative Safe Undo: git revert

[!IMPORTANT] Golden Rule of Git: Never use git reset on commits that have already been pushed to a shared public branch (like main)! Resetting rewrites history and breaks other developers' clones.

Instead, use git revert:

  • git revert does not erase the bad commit from history.
  • Instead, it creates a brand new commit that introduces the exact opposite mathematical inverse of the bad commit!
1Before Revert:
2(C1) ---> (C2: Introduced payment bug) ---> (C3) [main, origin/main]
3 
4Command: git revert C2
5 
6After Revert:
7(C1) ---> (C2: Bug) ---> (C3) ---> (C4: Revert "Introduced payment bug") [main]

Because history is purely additive, git revert is 100% safe for pushed production branches.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which command safely discards uncommitted changes in app.js in your working directory?
Question 2
What is the effect of running git reset --soft HEAD~1?
Question 3
Why is git reset --hard considered a dangerous command?
Question 4
If a buggy commit has already been pushed to a shared team branch on GitHub, what is the best command to undo it?
Question 5
What does git restore --staged <file> do?
Next Lesson

Rebasing Branches: Linear History with git rebase vs git merge

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Shelving Changes Temporarily with git stash (save, pop, apply, drop)Rebasing Branches: Linear History with git rebase vs git merge

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Git & GitHub Basics with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext