While developing software, making mistakes is completely normal.
You may accidentally:
Delete important code.
Modify the wrong file.
Add debugging statements.
Experiment with a feature that doesn't work.
Stage a file by mistake.
Fortunately, Git provides several ways to recover from these situations.
One of the safest and most beginner-friendly commands is git restore.
Introduced in Git 2.23, git restore was designed to make undoing changes easier and less confusing than older commands such as git checkout.
In this article, you will learn:
What
git restoreisWhen to use it
How to restore modified files
How to unstage files
The difference between restoring and deleting
The difference between
git restore,git checkout, andgit resetCommon mistakes and best practices
By the end of this article, you'll know how to safely discard unwanted changes without damaging your repository.
Why Do We Need git restore?
Imagine you have already committed this code:
public class Main {
public static void main(String[] args) {
System.out.println("Welcome");
}
}
Later, you accidentally change it to:
public class Main {
public static void main(String[] args) {
System.out.println("Wrong Output");
}
}
You don't want this change anymore.
Instead of manually editing the file, Git can restore the last committed version for you.
This is exactly what git restore is designed to do.
What is git restore?
The git restore command restores files to an earlier state.
It can:
Discard unwanted changes in the Working Directory.
Remove files from the Staging Area.
Restore files from a specific commit.
Unlike git commit or git add, git restore is an undo command.
It helps you recover from mistakes before they become permanent.
Basic Syntax
git restore <filename>
Example:
git restore Main.java
Git replaces the modified version of Main.java with the last committed version.
Understanding the Git Areas Again
Before using git restore, let us review Git's three main areas.
Working Directory
│
▼
Staging Area
│
▼
Local Repository
The behavior of git restore depends on where your changes currently exist.
Scenario 1: Restoring an Unstaged File
Suppose the latest committed file contains:
System.out.println("Welcome");
You modify it:
System.out.println("Welcome to Git");
Run:
git status
Output:
Changes not staged for commit:
modified: Main.java
You decide to discard these changes.
Run:
git restore Main.java
Now open the file.
It becomes:
System.out.println("Welcome");
The unwanted modification has disappeared.
What Happened?
Git copied the last committed version from the repository into the Working Directory.
The modified version was discarded.
The lifecycle looked like this:
Repository
│
▼
Working Directory
Modified File
↓
git restore
↓
Working Directory matches Repository
Checking Status After Restore
Run:
git status
Output:
nothing to commit, working tree clean
This means:
No modified files
No staged files
Working Directory matches the latest commit
Scenario 2: Restoring Multiple Files
Suppose your project contains:
Main.java
Student.java
Database.java
All three files have been modified.
Instead of restoring them one by one, run:
git restore Main.java Student.java Database.java
Git restores all selected files.
Scenario 3: Restoring Every Modified File
Suppose you accidentally modified dozens of files.
Instead of specifying every filename, use:
git restore .
The dot (.) means:
Current Directory
Git restores every modified tracked file in the current directory and its subdirectories.
Use this command carefully because it discards all unstaged modifications.
Does git restore Delete Files?
No.
It restores tracked files to a previous version.
Suppose you modify:
Main.java
Running:
git restore Main.java
does not delete the file.
It replaces the current contents with the previous version.
Can git restore Recover Deleted Files?
Suppose you accidentally delete:
Student.java
Git detects:
deleted: Student.java
Run:
git restore Student.java
Git restores the deleted file from the latest commit.
This works only if the file existed in the repository.
Scenario 4: Unstaging a File
Suppose you run:
git add Main.java
Later you realize:
"I don't want this file in the next commit."
Instead of deleting the file, simply remove it from the Staging Area.
Run:
git restore --staged Main.java
The file moves:
Staging Area
│
▼
Working Directory
The file still exists.
Your changes are still present.
Only the staging information is removed.
Understanding --staged
The option:
--staged
means:
Restore changes in the Staging Area.
Without --staged:
git restore Main.java
Git restores the Working Directory.
With:
git restore --staged Main.java
Git restores the Staging Area.
Combining Both Options
Suppose you want to:
Remove a file from staging.
Discard its modifications completely.
Run:
git restore --staged --worktree Main.java
The file returns to the latest committed version.
Both:
Working Directory
Staging Area
are restored.
Restoring From a Specific Commit
Git can restore a file from an older commit.
Example:
git restore --source=<commit-hash> Main.java
Suppose:
Commit A
↓
Commit B
↓
Commit C
You can restore Main.java exactly as it existed in Commit A.
This is useful when recovering older versions of files.
git restore vs git checkout
Before Git 2.23, developers commonly used:
git checkout Main.java
to discard changes.
The problem was that git checkout performed multiple unrelated tasks:
Switch branches
Restore files
This confused beginners.
Git introduced:
git switch
for switching branches.
and:
git restore
for restoring files.
Today, using git restore for file restoration is the clearer and recommended approach.
git restore vs git reset
These commands have different purposes.
| Command | Purpose |
|---|---|
git restore | Restore file contents |
git reset | Move branch pointers and optionally unstage or reset commits |
A simple way to remember:
git restorefocuses on files.git resetfocuses on history and commit positions.
We will study git reset in detail later.
Real-World Example
Suppose you are developing an Online Banking System.
You modify:
Login.java
Transfer.java
README.md
While testing, you accidentally break Transfer.java.
Instead of manually fixing it, run:
git restore Transfer.java
Git immediately restores the last committed version.
The other modified files remain unchanged.
Common Beginner Mistakes
Mistake 1
Running:
git restore .
without checking the repository.
This discards all unstaged modifications.
Always verify with:
git status
first.
Mistake 2
Thinking git restore deletes files.
It restores tracked files.
It is not a file deletion command.
Mistake 3
Expecting git restore to recover files that were never committed.
If a new file has never been committed, Git has no previous version to restore.
Mistake 4
Confusing:
git restore
with
git reset
These commands solve different problems.
Best Practices
Review changes with
git diffbefore restoring.Use
git statusto understand the current state.Restore individual files whenever possible.
Avoid using
git restore .unless you are certain.Commit important work before experimenting with risky changes.
Useful git restore Commands
| Command | Purpose |
|---|---|
git restore Main.java | Discard modifications in one file |
git restore file1 file2 | Restore multiple files |
git restore . | Restore all modified tracked files |
git restore --staged Main.java | Unstage a file |
git restore --staged --worktree Main.java | Unstage and discard changes |
git restore --source=<commit> Main.java | Restore a file from a specific commit |
Summary
The git restore command provides a safe and straightforward way to undo unwanted changes in Git. Whether you need to discard modifications in the Working Directory, remove files from the Staging Area, or restore files from an earlier commit, git restore offers a clear and focused solution.
Unlike older commands such as git checkout, git restore has a single purpose: restoring files. This makes it easier to learn and reduces the risk of accidental mistakes.
Understanding git restore is an important step toward becoming confident in Git, because it allows you to experiment freely, knowing that you can safely undo changes when necessary.
Comments
Post a Comment