Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do Git restore options differ for staged and unstaged changes?

Quick answer

git restore --staged <path> removes that path’s staged difference from the index by restoring it from HEAD; it leaves working-tree edits in place. git restore <path> restores the working-tree file from the index, so it can discard unstaged edits. Using both --staged and --worktree changes both locations. Check status and the relevant diffs before choosing.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have edits staged and unstaged in the same file. What does each restore option actually change?

Geez

Think of the index (staging area) and working tree (the file you edit) as two different states. A restore command targets one or both:

  • git restore --staged <path> restores the index entry from HEAD by default. It removes that path’s staged difference but leaves working-tree edits in place.
  • git restore <path> restores the working-tree file from the index by default. It can discard edits that exist only in the working tree.
  • git restore --staged --worktree <path> restores both locations. [S1]

For example, if notes.txt has changes in both states, the first command targets its staged difference; the second targets its unstaged working-tree difference. Use the path you intend to affect.

Follow-up question

How can I tell which changes are staged and which are not before I restore anything?

Geez

Check status and the relevant diffs before running a restore:

git status --short
git diff --cached -- notes.txt
git diff -- notes.txt

The cached diff compares staged changes with HEAD; the regular diff compares working-tree changes with the index. They show distinct states, so review the one—or both—that your planned command will affect. [S2]

If both diffs show changes for notes.txt, git restore --staged -- notes.txt targets the staged difference, while git restore -- notes.txt targets the unstaged working-tree difference. [S1] Check the path carefully, and pause to read the options if you are unsure what a command will change.

Follow-up question

I want to unstage a file but keep all my edits. What should I do, and how do I check that it worked?

Geez

If your goal is to remove the file’s staged difference while retaining its current working-tree edits, inspect both diffs first. Then run:

git restore --staged -- notes.txt

Replace notes.txt with the path you intend. By default, this restores the index entry from HEAD and leaves the working-tree edits in place. [S1]

Afterward, check git status --short, git diff -- notes.txt, and, if useful, git diff --cached -- notes.txt. These checks show the resulting states; they are not a guarantee that edits discarded by another operation can be recovered. [S2]

Follow-up question

What if I only want to throw away the unstaged edits, or reset both states? What’s a useful decision rule?

Geez

Choose based on which state you want to change and what you want it restored from:

  • To make the working-tree file match the index, use git restore -- <path>. This can discard unstaged edits. [S1]
  • To remove staged changes while keeping working-tree edits, use git restore --staged -- <path>. [S1]
  • To restore both the index and working tree, use git restore --staged --worktree -- <path>. [S1]

Before any of them, review git status --short, git diff --cached -- <path> for staged changes, and git diff -- <path> for unstaged changes. [S2] Confirm the path is exactly the one you intend, then inspect status and diffs afterward. The diffs help identify the states affected, but do not guarantee recovery of edits discarded by a restore. [S2]

A practical checklist

  • Run git status --short and identify the exact path you plan to affect.

  • Review git diff --cached -- <path> for staged changes.

  • Review git diff -- <path> for unstaged working-tree changes.

  • Choose whether to change the index, working tree, or both.

  • Read the command and path carefully; inspect status and diffs afterward.

A few more useful answers

Does `git restore --staged <path>` erase my unstaged edits too?

By default, no: it restores the index entry from HEAD and leaves working-tree edits in place. Check both diffs to distinguish staged and unstaged changes. [S1] [S2]

Can `git restore <path>` discard changes I have not staged?

Yes. Without --staged, it restores the working-tree file from the index by default, which can discard unstaged edits. Inspect git diff -- <path> before using it. [S1] [S2]

Does checking a diff guarantee I can recover changes after restoring?

No. The diffs help you identify staged and unstaged changes before acting, but they do not guarantee recovery of discarded edits. [S2]

Sources

References used for this guide.

  1. [S1] Git - git-restore Documentation
    git-scm.com · Checked
  2. [S2] Git - git-diff Documentation
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work