Home Coding & Scripting Undoing Changes with git restore

Undoing Changes with git restore

173
0

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 restore only 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.

Previous articleExploring Commit History with git log
Next articleUndoing Commits with git reset
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here