Coding/Learn/git/git restore and revert
Git & GitHub Complete Course
01.Introduction, Architecture & Configuration
02.Staging Area, atomic Commits & git status
03.Branching, Fast-Forward & 3-Way Merging
04.Stash, Log Visualization & History Inspection
05.Workflows (Git Flow, Trunk-Based) & Semantic Tagging
06.SSH Keys, Remote Repositories & Origin Setup
07.Push, Fetch, Pull & Upstream Tracking Branches
08.Forking, Clones & Open-Source Pull Requests (PRs)
09.GitHub Flow, Branch Protection & GitHub Pages
10.Undoing Changes: git restore & Safe Public git revert
11.Reset: Deep Dive into --soft, --mixed & --hard
12.Commit Amending & Interactive Rebase (git rebase -i)
13.Reflog & Disaster Recovery (Reviving "Lost" Commits)
14.Merge Conflicts Resolution & git rerere
15..gitignore Rules & .gitattributes Cross-Platform Handling
16.Hooks & Automated Quality Assurance (Pre-Commit)
17.Submodules & Large File Storage (Git LFS)
18.GPG Cryptographic Signing & git cherry-pick
19.Advanced Remotes, Garbage Collection (git gc) & Maintenance
20.Capstone: Production Multi-Branch Enterprise Release Pipeline
Course Certificate0%

Complete all lessons to earn

LearnUndoing Changes: git restore & Safe Public git revert
10 / 20 0%
Lesson 10 of 20
6 mins read

Undoing Changes: git restore & Safe Public git revert

PreviousNext Lesson
In Plain English

Git provides dedicated commands for undoing mistakes at every stage. git restore discards uncommitted modifications in the working tree or un-stages files from the index. git revert creates a brand new inverse commit that cancels out a previous bad commit without rewriting history, making it completely safe for shared branches.

The Mental Model

"git restore is pressing Ctrl+Z on an unsaved draft; git revert is issuing an official accounting refund check that credits your account without erasing the original invoice from the ledger."

Deep Dive: How It Works

git restore <file>: Discards unstaged modifications in working directory, resetting file to staged/HEAD state.

git restore --staged <file>: Removes a file from the staging index back to working directory.

git revert <commit-SHA>: Computes the inverse diff of the target commit and creates a new commit.

Syntax Blueprint

git restore main.c          # Discard unstaged changes
git restore --staged main.c # Unstage file
git revert HEAD             # Safely invert last commit

Use git restore for local uncommitted changes and git revert for shared public history.

PointCore Rules to Remember

Point star
Safe for Public Branches: git revert preserves linear history and never breaks teammate clones or CI builds.
Point star
Precise Target Reverts: You can revert any commit deep in the history graph by specifying its SHA checksum.
Point star
No History Rewriting: Both the original bad commit and the revert commit remain visible in git log audits.

Common Beginner Traps & How to Fix Them

Mistake:Running git reset --hard on a commit that has already been pushed to origin/main.

Why it happens: Destroys history on your machine, causing force-push rejection and teammate conflicts.

How to fix: Always use git revert on commits that exist on remote shared branches.

Pro Tip: Use git revert -n (no-commit) when you want to revert multiple commits into a single consolidated rollback commit.

Live Interactive Example

Hit Run Code to see it live
Safe Public Revert
Git 2.45 / CLI
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: git revert creates a new commit that restores app.txt back to its stable state.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Revert Faulty Commit

  • PointRevert the latest commit using "git revert HEAD --no-edit".
  • PointDisplay the commit history with "git log --oneline".
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates