Discard unstaged changes in Git

Restore tracked files from the index while keeping staged edits, preview untracked cleanup, or stash changes you may want to recover later.

Best practices
Illustration of Git branches and version history

Use git restore -- file to discard a tracked file's unstaged changes. By default, it copies the version from the index, so staged changes remain intact. It does not necessarily restore the last commit's version.

git diff -- src/app.js
git restore -- src/app.js

Inspect the diff first. Work that was never saved in a commit, stash, or another backup has no dependable Git recovery path after it is overwritten.

Choose the scope

# One tracked file
git restore -- src/app.js

# A directory
git restore -- src/

# The current directory and its descendants
git restore -- .

# All tracked working-tree paths, even from a subdirectory
git restore -- :/

Run these in an ordinary resolved working tree. An unmerged index needs conflict-specific handling. Before Git 2.23, git checkout -- path provides the familiar index-to-working-tree form.

Staged and untracked files are separate cases

To unstage changes while retaining their working-tree contents:

git restore --staged -- .

To discard both staged and unstaged changes in tracked paths, use git reset --hard HEAD only when that loss is intended. It can also delete untracked files or directories that obstruct writing tracked paths; it does not generally clean every unrelated untracked file.

For untracked files and directories, preview before cleaning:

git clean -nd
# After reviewing the proposed removals:
git clean -fd

Ignored files are normally excluded. Adding -x expands deletion to ignored contents, so it can remove local configuration, dependencies, or generated assets as well.

Save changes instead of discarding them

git stash push -m "Work before trying another approach"

The default stash includes tracked changes. Add -u when you also need untracked files, or arrange a separate backup for ignored files. Restore a stash with git stash apply or git stash pop; applying it can produce conflicts.

FAQ

Can Git recover edits after restore?

Only if another copy remains somewhere, such as a commit, stash, or editor history. Do not rely on Git to recover arbitrary unstaged text it never recorded.

Are git checkout -- . and git restore . equivalent?

For normal, resolved tracked paths, both restore from the index within the current directory's scope. Neither is a command to clear staged changes automatically.

Why does clean require force?

The usual clean.requireForce setting protects untracked data from accidental removal. Use -n to inspect candidates even when you know you will eventually use -f.

See git-restore, git-clean, git-reset, and git-stash.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting