PREP WITH PROOF
Menu
GIT & DEVELOPER WORKFLOWS / PRACTICAL GUIDE

Git Restore: Unstage Changes vs Discard Working Edits

Choose git restore safely with a tested lab for staged and working changes, HEAD sources, MM status, new files, path errors and unchanged commit history.

THE QUICK ANSWER

Use git restore --staged -- path to unstage while keeping the working file. Plain git restore -- path replaces working content from the index. To replace both from the current commit, select --source=HEAD --staged --worktree explicitly after preserving needed edits.

An accidental stage and an unwanted edit are different problems

You staged a configuration change, continued editing the file, and now want to remove it from the next commit. A colleague suggests git restore. Before running it, identify which version you want to replace: the staged snapshot or the file currently open in your editor. The same filename can represent three different contents at once.

This guide focuses on ordinary tracked files in a repository with an existing commit and no unresolved merge. It uses one file containing base in HEAD, staged in the index, and working on disk. That deliberately small example makes each operation observable. No remote, credentials or project dependency installation is required.

The original Bash lab below checks file contents and commit identity after every operation. Run it only as a complete script in a disposable environment; it creates its own temporary repository. The later command templates describe real-project decisions, but copying them without inspecting your changes can discard work.

Read all three versions before deciding what to undo

HEAD names the current commit in this example. The index contains the proposed file snapshot for the next commit. The working tree is the editable file on disk. Staging takes a snapshot; another edit does not automatically refresh that snapshot. Consequently, a file can have both staged and unstaged changes.

The first diff below compares the index with the working file. The cached diff compares HEAD with the index. The final diff compares HEAD directly with the working file. Read the two boundaries separately: a clean first diff does not establish that nothing is staged. Likewise, an empty cached diff does not establish that the editor matches HEAD.

Inspecting a particular snapshot with git show is useful when a large diff obscures which value is actually stored. In this fixture, HEAD:note.txt returns base, :note.txt returns staged, and cat returns working. These are local observations of different storage locations, not three names for the same latest file.

git status --short
git diff -- note.txt
git diff --cached -- note.txt
git diff HEAD -- note.txt
git show HEAD:note.txt
git show :note.txt
cat note.txt

Source: Git diff: working, cached and commit comparisons ↗

Choose a destination and a source explicitly

The command matrix assumes every row starts from the same fixture: HEAD=base, index=staged, working file=working. Read each row independently. The default destination is the working tree. Adding --staged selects the index and also changes the default source to HEAD. --source selects the content to copy; it does not select the destination.

None of these path-restoration operations creates a commit or moves HEAD. The differences are which file snapshot is replaced and what remains available afterward. In real work, use one narrow path first and inspect both diffs again before widening the selection.

Command for note.txtIndex afterwardWorking file afterwardMeaning in this fixture
git restore -- note.txtstagedstagedDiscard only working edits since staging
git restore --staged -- note.txtbaseworkingUnstage; preserve current disk content
git restore --source=HEAD --worktree -- note.txtstagedbaseReplace disk content from HEAD; keep staged snapshot
git restore --source=HEAD --staged --worktree -- note.txtbasebaseReplace both snapshots from HEAD

Source: Git restore: sources, destinations and pathspec options ↗

Unstage a file while keeping the editor content

For the common request “keep my changes, but leave this file out of the next commit,” select only the index. In the lab, unstaging replaces staged with base in the index, while working remains on disk. The result is an unstaged difference between base and working.

Notice what preservation means here. The current working file survives. The intermediate staged snapshot is no longer selected for the next commit. If that earlier snapshot contains useful content absent from the working file, preserve it separately before unstaging. Keeping the editor content is not a guarantee that every intermediate version stays easy to retrieve.

A newly added file has a related outcome. The lab stages draft.txt, then unstages it. Because HEAD has no draft.txt, the index entry disappears and the file remains on disk as untracked. This changes its tracking state without deleting its contents. Review untracked files explicitly when preparing the next commit.

# Inspect first; run from the repository containing this path.
git diff --cached -- note.txt
git restore --staged -- note.txt
git diff --cached -- note.txt
git diff -- note.txt
git status --short -- note.txt

Source: Git reset: path and commit forms ↗

Discard working edits without erasing the staged snapshot

In the initial fixture, plain git restore copies staged back to disk. The working edit is replaced, but the staged snapshot remains staged. This is useful when you intentionally staged the version you want to keep and then introduced an unwanted experiment in the editor.

If you expected base, plain restore can look as though it failed. It restored from the index, which contained staged. Select HEAD explicitly when the committed version is the desired source. Keeping --worktree visible in that command makes its destination easier to review.

The less obvious result is index=staged and working=base. Status reports MM because each boundary differs: HEAD to index and index to working tree. The two changes can even cancel when you compare HEAD directly with the working file. An empty git diff HEAD is therefore insufficient proof that both snapshots are clean.

Source: Git status: staged, working and unmerged states ↗

Run the complete disposable repository experiment

Save this block as restore-lab.sh and run bash restore-lab.sh. It prints the temporary directory, checks each scenario from a freshly seeded staged/working pair, and retains the repository for inspection. The script uses repository-local identity settings and makes one local baseline commit. It does not contact a remote or change your global Git configuration.

Success ends with PASS: 26 checks. A failing comparison stops the script with a named check. Command substitutions in these tiny assertions remove final newlines; that is sufficient for the three single-line markers, but it is not a general byte-for-byte file comparator. The additional publication checks use byte comparisons for varied text contents.

The experiment verifies ordinary file snapshots, the newly added file case, and unchanged HEAD identity. It does not simulate merge conflicts, sparse checkouts, submodules, custom filters or platform-specific permission behavior. Keep those environments separate from conclusions drawn from this fixture.

#!/usr/bin/env bash
set -eu
lab_dir=$(mktemp -d "${TMPDIR:-/tmp}/pwp-restore.XXXXXX")
printf 'Disposable repository: %s\n' "$lab_dir"
cd "$lab_dir"
git init -q
git config user.name 'Restore Lab'
git config user.email 'restore-lab@example.invalid'
git config commit.gpgsign false
git config core.autocrlf false
printf 'base\n' > note.txt
git add -- note.txt
git commit -qm 'Base snapshot'
base_commit=$(git rev-parse HEAD)
checks=0
check() {
  test "$1" = "$2" || { printf 'FAIL: %s\n' "$3" >&2; exit 1; }
  checks=$((checks + 1))
}
seed() {
  printf 'staged\n' > note.txt
  git add -- note.txt
  printf 'working\n' > note.txt
}
state() {
  check "$(git show HEAD:note.txt)" base 'HEAD content'
  check "$(git show :note.txt)" "$1" 'index content'
  check "$(cat note.txt)" "$2" 'working content'
  check "$(git rev-parse HEAD)" "$base_commit" 'HEAD identity'
}
seed
state staged working
git restore -- note.txt
state staged staged
check "$(git status --porcelain=v1 -- note.txt)" 'M  note.txt' 'staged only'
seed
git restore --staged -- note.txt
state base working
check "$(git status --porcelain=v1 -- note.txt)" ' M note.txt' 'unstaged only'
seed
git restore --source=HEAD --worktree -- note.txt
state staged base
check "$(git status --porcelain=v1 -- note.txt)" 'MM note.txt' 'both comparisons differ'
seed
git restore --source=HEAD --staged --worktree -- note.txt
state base base
check "$(git status --porcelain=v1 -- note.txt)" '' 'clean path'
printf 'draft\n' > draft.txt
git add -- draft.txt
git restore --staged -- draft.txt
check "$(cat draft.txt)" draft 'unstaged new file survives'
check "$(git status --porcelain=v1 -- draft.txt)" '?? draft.txt' 'new file is untracked'
printf 'PASS: %s checks\n' "$checks"
printf 'Repository retained for inspection: %s\n' "$lab_dir"

Restore selected hunks when a whole-file decision is too broad

A file can combine an intended correction with unrelated debugging. Inspect the complete diff before choosing a whole-file operation. Patch mode provides an interactive hunk selection workflow. Its source and destination choices still matter; selecting a small region does not reverse their direction.

For example, git restore --staged --patch -- note.txt offers staged differences for selective unstaging. git restore --patch -- note.txt selects working-tree restoration relative to the index. Read each prompt and proposed change rather than treating the key sequence as a reusable script. Adjacent edits may be grouped together, so hunk selection is not always equivalent to selecting individual semantic changes.

The noninteractive lab does not test patch prompts. When a hunk cannot express the intended result clearly, editing the file deliberately and staging the desired result can be easier to review. Recheck the cached diff before committing.

Source: Git restore: sources, destinations and pathspec options ↗

Explain path errors and missing files before broadening the command

Use -- before the path to end option parsing, and quote paths containing spaces. Git still interprets pathspec syntax; -- does not make every character literal. For a filename that contains wildcard characters, the global --literal-pathspecs option expresses literal matching. Verify the selected path before using a directory-wide restore.

A pathspec error can mean you are in the wrong directory, the path is not tracked in the relevant snapshot, or the spelling differs. It is not a reason to rerun restore against the entire repository. An ordinary untracked file has no index snapshot for plain restore to recover. An ignored file is another case to diagnose separately.

A tracked path absent from an explicitly selected source can be removed to match that source. Restoring from an older commit is therefore a content replacement decision, not a promise that every selected file will exist afterward. Inspect the source tree and preserve needed content before applying it.

# Read-only tracking inspection, including a filename with spaces.
git ls-files --error-unmatch -- 'notes draft.txt'
# Literal pathspec inspection for a filename containing brackets.
git --literal-pathspecs ls-files --error-unmatch -- 'notes[1].txt' 

Source: Git restore: sources, destinations and pathspec options ↗

Keep a recoverable checkpoint when the decision is uncertain

Before replacing valuable edits, save a deliberate checkpoint and verify it. A temporary commit on an appropriate local branch or an independent copy can preserve content that has never been committed. Avoid putting secrets into commits merely to create a checkpoint; choose an appropriate storage boundary for the material.

A stash is another local option, but inspect what it contains rather than assuming every file was captured. The include-untracked option covers untracked files; ignored files need separate consideration. Applying a stash can conflict, and a named stash should remain available until you verify the recovered content.

Do not assume reflog can reconstruct text that existed only in an editor or working file. Reference-history recovery and file-content restoration solve different problems. If a command has already discarded an uncommitted edit, stop further cleanup and inspect editor history or verified backups before making broader repository changes.

Source: Git stash: saving and applying local changes ↗

Verify the intended next commit, not just a clean-looking editor

After a restore, inspect status plus both diffs, then run the checks relevant to the changed code. If your goal was unstaging, confirm that the cached diff excludes the path and the working content still contains the edits you need. If your goal was discarding an experiment, confirm that the remaining staged change is still the one you intend to commit.

Use restore for selected file snapshots. A path-form reset can also update staged paths; a commit-form reset can move the branch tip. Revert records a new commit reversing earlier changes and is a different choice when a committed change must be undone in shared history. Do not replace a one-file staging decision with a broader history operation merely because both are described as undo.

The useful habit is to name the source snapshot, destination snapshot and expected remaining diff before running the command. The lab gives you four concrete outcomes to compare with your own repository. When the result differs, inspect the actual index and path state first; repeating the same command with a wider pathspec usually hides the evidence you need.

Source: Git revert: reversing committed changes ↗

KEEP THESE THREE THINGS

Your next steps

  1. Inspect HEAD, the index and the working file separately; read both git diff and git diff --cached.
  2. Choose the restore source and destination deliberately, and preserve valuable intermediate content before replacement.
  3. Verify the remaining staged snapshot and tests before committing; the four lab restore cases keep HEAD unchanged.

Sources & official resources

Linked resources checked on 2026-11-06. Technical behavior and provider guidance can change; check the documentation for the version you use.

About this guide and its recommendations

Published by Prep With Proof with AI-assisted drafting. Study routines and examples are editorial suggestions, not official exam instructions or claims of a learner's personal results. Cited provider facts are linked; ownership of recommended Prep With Proof products is disclosed. No competitor endorsement or guaranteed certification outcome is implied.

Suggest a correction →