In the post Undoing Changes in Git: restore, reset, revert, we learned that Git provides multiple ways to undo changes: restore, reset, and revert.
Among these, git restore is the safest command for beginners, because it works only with uncommitted changes. It helps us undo modifications in our working directory or staging area without affecting commit history.
In this hands-on guide, we will practice using git restore step by step.
Before We Begin
Run the commands in this guide inside a terminal and within a Git repository folder.
- Windows: Open Git Bash, PowerShell, or Windows Terminal
- macOS / Linux: Open the Terminal
1. When Do We Use git restore?
We use git restore when we want to undo changes that have not been committed yet.
Typical scenarios include:
- We edited a file but want to discard the changes.
- We accidentally staged a file and want to unstage it.
- We want to restore a file back to the version in the last commit.
Important:
git restoreonly affects the working directory and staging area, not the commit history.
2. Setting Up a Practice Repository
Let’s create a small repository to experiment safely.
mkdir restore-demo
cd restore-demo
git init
Create a file:
echo "Version 1" > notes.txt
Add and commit it:
git add notes.txt
git commit -m "Initial commit"
Our repository now contains one committed file.
3. Scenario 1 — Undo Changes in a File
Modify the file:
echo "Version 2 change" >> notes.txt
Check the status:
git status
Git will show that notes.txt has been modified.
View the changes:
git diff
Now suppose we decide we don’t want these changes anymore.
Run:
git restore notes.txt
Check the file:
cat notes.txt
We will see that the file has been restored to its previous committed state.
4. Scenario 2 — Undo Multiple File Changes
Create another file:
echo "Temporary content" > temp.txt
Modify the first file again:
echo "Another change" >> notes.txt
Check status:
git status
If we want to discard changes in all modified files, we can run:
git restore .
The . tells Git to restore all files in the current directory.
5. Scenario 3 — Unstage a File
Sometimes we stage a file accidentally.
Modify the file again:
echo "Another edit" >> notes.txt
Stage it:
git add notes.txt
Check status:
git status
Git will show the file in the staging area.
To move it back to the working directory (unstage it):
git restore --staged notes.txt
Check status again:
git status
The file will now appear as modified but not staged.
6. Scenario 4 — Restore a File from a Specific Commit
First, make another commit:
git add notes.txt
git commit -m "Second commit"
Now modify the file again:
echo "Third change" >> notes.txt
Suppose we want to restore the file to an earlier commit.
First check commit history:
git log --oneline
Copy the commit ID.
Then restore the file from that commit:
git restore --source=<commit-id> notes.txt
The file will now match the version stored in that commit.
7. Understanding What git restore Affects
git restore works on two areas:
Working Directory
Where our files currently exist.
Example:
git restore file.txt
Restores the file to the last committed version.
Staging Area
Where files are prepared for the next commit.
Example:
git restore --staged file.txt
Removes the file from the staging area.
8. Quick Command Reference
Restore changes in a file:
git restore file.txt
Restore all files:
git restore .
Unstage a file:
git restore --staged file.txt
Restore a file from a specific commit:
git restore --source=<commit-id> file.txt
9. Summary
git restore is the safest way to undo changes before committing.
We use it to:
- Discard unwanted file modifications
- Restore files to their last committed state
- Unstage files accidentally added with
git add - Recover file versions from previous commits
Unlike git reset, it does not modify commit history, making it ideal for everyday development mistakes.