Git: How to Discard Local Changes

Git has plenty of different ways to let you discard your local, uncommitted changes depending on the status of files in your project. This guide looks at some of the most common use cases, including discarding changes to staged, unstaged, and untracked files.

How to discard and undo local, uncommitted changes

If you have local changes that you have not staged yet and want to get rid of completely, you can use two different commands.

Using git restore

git restore is used to discard untracked local changes.

  • $ git restore <file>: Replace <file> with your relative file path to discard a single file
  • $ git restore .:  If you replace the <file> part of the command with a period, git will discard all local unstaged changes from your local setup

Using git checkout

To revert all changes made to your local working directory you can also use the git checkout command as follows:

$ git checkout .

How to discard local changes: untracked and new files

If you want a completely clean slate and want to discard all your local, uncommitted changes (including files you may have just created), you can use git clean.

$ git clean -d

Here, the -d flag is used to include untracked directories and untracked files. If you have made a lot of new local changes and want to remove whole directories from your local changes you will need to add the -d flag. Without this flag, git will only remove untracked files and not folders.

💡
If you have made a lot of changes, you can also do a dry run with this command to see exactly what files will be deleted

To do a dry run before you go about deleting all your files, simply use the following git command:

$ git clean -nd

How to discard local changes: staged and unstaged files

If you want to remove all your changes including both staged and unstaged files (but not including new, untracked files), you can use the following command:

$ git reset --hard

This will revert your project to your last commit and remove all tracked files from your working directory.