Git Foundations / Undo Changes Safely

Discard and unstage changes

Lesson 10 of 33. Open to read. No account needed.

Two Ways to Back Out

git status has been showing two hints next to its lists of changes:

(use "git restore --staged <file>..." to unstage)
(use "git restore <file>..." to discard changes in working directory)

They describe two different jobs:

  • Unstage: take a change out of the next commit, but keep it in your file. Nothing is lost.
  • Discard: throw away an edit in your file and go back to an earlier version. The edit is gone.

Both use git restore. The difference is which version it copies, and where.

How Restore Copies Versions

Three areas stacked from top to bottom: the working tree with your latest edits, the staging area with the version you staged, and the last commit. An arrow labelled git restore file copies the staged version up into the working tree, replacing your edit, so the unstaged edit is lost. An arrow labelled git restore --staged file copies the committed version up into the staging area, which unstages the file while your edit stays in the working tree. Neither command changes the last commit.

Read the arrows as "copy this version over that one":

  • git restore <file> copies the staging area's version of the file over the one in your working tree. If you have not staged the file, the staging area still holds the last commit's version, so all your edits to it are discarded. If you staged an edit, that edit survives: only changes made after staging are discarded.
  • git restore --staged <file> copies the last commit's version into the staging area. That unstages the file: your edit stays in the working tree, now listed under "Changes not staged for commit". A new file has no version in the last commit, so it leaves the staging area and becomes untracked again. It stays on your disk.

To discard staged and unstaged edits to a file in one step, name both places:

git restore --staged --worktree <file>

None of these commands changes a commit. They only move versions between the last commit, the staging area and your files.

What Cannot Be Brought Back

Unstaging is always safe. Discarding is not: an edit you never committed is not stored anywhere in Git's history, so once git restore discards it, no Git command brings it back. Your editor's undo history is the only hope.

Before discarding, look at exactly what you will lose:

git diff <file>

Every change that diff shows is what git restore <file> throws away. Take extra care with git restore ., which discards the unstaged edits in every file in the current folder and the folders inside it.

Committed work is different: once a version is in a commit, later lessons show several ways to get it back.

Older Commands You May See

git restore arrived in Git 2.23, which split these jobs out of the older git checkout command. Older guides discard edits with git checkout -- <file> and unstage with git reset HEAD <file>. Both still work, but this course uses git restore, whose name says what it does.

Exercise: Unstage One Change and Discard Another

You ran git add . and staged three changes at once: a new book for the reading list, which you want; a line you deleted from the schedule by accident; and a personal note that should never be committed. You will keep the first, discard the second and unstage the third without deleting it.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup:

    mkdir -p ~/git-practice/restore-practice
    cd ~/git-practice/restore-practice
    git init -b main
    cat > books.md <<'EOF'
    # Reading list
    
    - Paper Lanterns
    - A Year of Rain
    EOF
    cat > schedule.md <<'EOF'
    # Book club schedule
    
    Meet on the first Sunday of each month.
    Location: the library meeting room.
    EOF
    git add books.md schedule.md
    git commit -m "Start the book club notes"
    
  2. Make the three changes and stage them all at once:

    cat >> books.md <<'EOF'
    - The Quiet Harbour
    EOF
    cat > schedule.md <<'EOF'
    # Book club schedule
    
    Meet on the first Sunday of each month.
    EOF
    cat > notes-to-self.txt <<'EOF'
    Ask Ravi about snacks.
    EOF
    git add .
    git status
    

    All three files are under "Changes to be committed":

    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
    	modified:   books.md
    	new file:   notes-to-self.txt
    	modified:   schedule.md
    
  3. Unstage the personal note, and check that it still exists:

    git restore --staged notes-to-self.txt
    git status
    ls
    

    notes-to-self.txt moves to "Untracked files", and ls still lists it.

  4. Unstage the schedule change:

    git restore --staged schedule.md
    git status
    

    schedule.md moves to "Changes not staged for commit". The deleted line is still missing from the file: unstaging kept your edit.

  5. Before discarding, check what you will lose:

    git diff schedule.md
    

    The only change is -Location: the library meeting room., the line you deleted by accident. Discarding the edit brings it back.

  6. Discard the edit:

    git restore schedule.md
    git status
    cat schedule.md
    

    schedule.md is no longer listed, and cat shows the location line again.

  7. Commit the change you wanted:

    git commit -m "Add The Quiet Harbour to the reading list"
    git status
    

    The commit summary says 1 file changed, 1 insertion(+). git status lists only notes-to-self.txt, still untracked.

Success: the commit contains only the books.md change, schedule.md matches the last commit, and notes-to-self.txt is still on your disk, untracked.

If something differs:

What you seeLikely causeNext step
git restore <file> prints nothing and the edit is still thereThe edit is staged, and restore copies the staged version, which already has itgit restore --staged <file>, then git restore <file>
did not match any file(s) known to gitA typo, or the file is untracked: restore needs a version Git has storedCheck the name in git status; an untracked file you no longer want can simply be deleted
A new file moved from "Changes to be committed" to "Untracked files"Expected after git restore --staged: the last commit has no version of itNothing to fix; it is still on your disk
Edits are gone that you meant to keepgit restore discarded them, and Git never stored themTry your editor's undo; next time read git diff <file> first
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf restore-practice
    ls
    

Independent task: before cleaning up, try two more repairs:

  1. Add a line to books.md and stage it:

    cat >> books.md <<'EOF'
    - Small Engines
    EOF
    git add books.md
    

    Now remove the new line completely, so books.md matches the last commit. Try git restore books.md first and explain what happens.

  2. Delete schedule.md by mistake with rm schedule.md, then bring it back using Git.

Check Your Understanding

  1. Which of these keeps your edit in the file, and which can lose work: git restore --staged notes.md or git restore notes.md?
  2. You stage an edit to a.txt, edit a.txt again, then run git restore a.txt. What does the file contain afterwards, and what is still staged?

Answer and Reasoning

Independent task

  1. git restore books.md changes nothing, because it copies the staged version, which already contains the new line. Unstage first, then discard:

    git restore --staged books.md
    git restore books.md
    

    Or do both in one step with git restore --staged --worktree books.md. Either way, git status no longer lists books.md.

  2. After rm schedule.md, git status lists deleted: schedule.md under "Changes not staged for commit". git restore schedule.md copies it back from the staging area, which still holds the committed version.

Understanding check

  1. git restore --staged notes.md keeps your edit: it only takes the change out of the next commit. git restore notes.md replaces the file with the staged version, and unstaged edits it discards are lost for good.
  2. The file goes back to the staged version: it keeps the first edit and loses the second. The first edit is still staged, because git restore without --staged never touches the staging area.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy