Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Look Before You Discard

Every command in this guide deletes work that Git has no copy of. First see what you are about to lose:

git status
git diff
git diff --staged

git diff shows unstaged edits, git diff --staged shows staged ones, and git status also lists untracked files. If any of it might be useful later, stash it instead, as shown at the end.

Choose by Case

CaseCommand
Edits in one filegit restore app.js
Edits in all filesgit restore .
Staged editsgit restore -S -W app.js
Untracked filesgit clean -fd
Back to last commitgit reset --hard
Not suregit stash -u

Each line is explained below, with the warning that belongs to it.

Discard Edits in One File or All Files

Warning: these commands overwrite uncommitted edits, and the edits cannot be recovered.

git restore app.js
git restore .

The first line resets one file to its staged version, or to the last commit if nothing is staged. The second does the same for every tracked file under the current folder. Older Git habits use git checkout -- app.js for the same job; see git checkout: switch branches and restore files.

Discard Staged Changes

To only take a file out of the staging area and keep the edits, use git restore --staged app.js, as explained in how to unstage files in Git. To unstage and discard in one step, restore both the index and the working tree.

Warning: the edits in the file are lost.

git restore --staged --worktree app.js

-S -W is the short form of the same options.

Remove Untracked Files

git restore does not touch files Git has never tracked. git clean removes them. Preview first:

git clean -n -d

Warning: git clean deletes files from disk without a copy anywhere. Run it only after the preview shows nothing you want to keep.

git clean -fd

Files matched by .gitignore stay unless you add -x, which would also remove local configuration and build output. More options are in Git remove untracked files with git clean.

Everything Back to the Last Commit

Warning: reset --hard discards every uncommitted change in tracked files, staged or not. Uncommitted work cannot be recovered afterward.

git reset --hard

Add git clean -fd after it if untracked files should go as well. Commits are not affected; to remove the last commit too, see how to undo the last commit in Git.

Match the Remote Branch Exactly

Sometimes a clone on a server has drifted and should look exactly like the remote branch again.

Warning: this discards uncommitted changes and also every local commit on the branch that is not on the remote.

git fetch origin
git reset --hard origin/main

Local commits removed this way can still be found in git reflog for a while, but uncommitted edits cannot. Check git log origin/main..main first: if it lists commits, decide whether to push them before you reset.

Not Sure? Stash Instead

git stash push -u -m "before cleanup"

This saves tracked and untracked changes and leaves a clean working tree, which looks the same as discarding. If you need the work later, git stash pop brings it back; if not, git stash drop removes it. Stash in detail: how to use git stash.

Was this answer helpful?
Loading...