[git] git: undo all working dir changes including new files

How to delete all changes from working directory including new untracked files. I know that git checkout -f does that, but it doesn't delete new untracked files created since last commit.

Does anybody have an idea how to do that?

This question is related to git

The answer is


git clean -i will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.

See git help clean for more information, including some other useful options.


For all tracked unstaged files use:

git checkout -- .

The . at the end is important.

You can replace . with a sub-directory name to clear only a specific sub-directory of your project. The problem is addressed specifically here.


You can do this in two steps:

  1. Revert modified files: git checkout -f
  2. Remove untracked files: git clean -fd

Have a look at the git clean command.

git-clean - Remove untracked files from the working tree

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.


This is probably a noob answer, but: I use TortoiseGit for windows and it has a nice feature called REVERT. So what you do to revert your local nonstaged nonpushed changes is:

  1. bring up a context menu for the needed folder and select revert, it shows a revert popup where you can select changed files to revert/recover.
  2. If you also want to delete added files(that are not in git yet) click commit (from same context menu) it brings up the Commit popup and shows you the added files, then right click each of them and choose delete. But dont press Commit btn in this popup, as you dont wanna commit, but only see added files and delete them from here.

Safest method, which I use frequently:

git clean -fd

Syntax explanation as per /docs/git-clean page:

  • -f (alias: --force). If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.
  • -d. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

As mentioned in the comments, it might be preferable to do a git clean -nd which does a dry run and tells you what would be deleted before actually deleting it.

Link to git clean doc page: https://git-scm.com/docs/git-clean


An alternative solution is to commit the changes, and then get rid of those commits. This does not have an immediate benefit at first, but it opens up the possibility to commit in chunks, and to create a git tag for backup.

You can do it on the current branch, like this:

git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01  # optional
git revert HEAD
git reset HEAD^^

Or you can do it on detached HEAD. (assuming you start on BRANCHNAME branch):

git checkout --detach HEAD
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01  # optional
git checkout BRANCHNAME

However, what I usually do is to commit in chunks, then name some or all commits as "DISCARD: ...". Then use interactive rebase to remove the bad commits and keep the good ones.

git add -p  # Add changes in chunks.
git commit -m"DISCARD: Some temporary changes for debugging"
git add -p  # Add more stuff.
git commit -m"Docblock improvements"
git tag archive/local-changes-2015-08-01
git rebase -i (commit id)  # rebase on the commit id before the changes.
  # Remove the commits that say "DISCARD".

This is more verbose, but it allows to review exactly which changes you want to discard.

The git lol and git lola shortcuts have been very helpful with this workflow.


The following works:

git add -A .
git stash
git stash drop stash@{0}

Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.

A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.

Credits: https://stackoverflow.com/a/52719/246724


For a specific folder I used:

git checkout -- FolderToClean/*

If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig. For instance:

[alias]
    discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"

Usage: git discard


I thought it was (warning: following will wipe out everything)

$ git reset --hard HEAD
$ git clean -fd

The reset to undo changes. The clean to remove any untracked files and directories.


git reset --hard origin/{branchName}

It will delete all untracked files.