Git

Discard All Local Changes in the Working Directory

If you want to discard all uncommitted changes (modifications and untracked files) in your working directory:

1
git reset --hard

If you also have untracked files and want to delete them:

1
git clean -fd
  • -f forces the cleaning.
  • -d removes untracked directories.

Discard Changes in a Specific File

If you only need to discard changes in a particular file:

1
git checkout -- filename

Or, in recent versions of Git:

1
git restore filename

Discard All Changes in Tracked Files

To discard all changes in tracked files without affecting untracked files:

1
git restore .

Revert Changes in the Staging Area

If you’ve already added files to the staging area (git add) but haven’t committed yet and want to remove them from there:

1
git restore --staged filename

Delete the Last Commit

If you’ve made a commit but haven’t pushed it yet:

1
git reset --soft HEAD~1

This undoes the last commit while keeping the changes in your working directory.

If you want to undo the commit and delete the changes from the working directory:

1
git reset --hard HEAD~1