[git] git reset --hard HEAD leaves untracked files behind

When I run git reset --hard HEAD, it's supposed to reset to a pristine version of what you pulled, as I understand it. Unfortunately, it leaves files lying around, as a git status shows a big list of untracked files.

How do you tell git "Just bring it back to EXACTLY what was in the last pull, nothing more, nothing less"?

This question is related to git

The answer is


The command you are looking for is git clean


You can use git stash. You have to specify --include-untracked, otherwise you'll end up with the original problem.

git stash --include-untracked

Then just drop the last entry in the stash

git stash drop

You can make a handy-dandy alias for that, and call it git discard for example:

git config --global alias.discard "! git stash -q --include-untracked && git stash drop -q"

git-clean Use to remove untracked files in the working tree. Following are some options (in brief) that can use with git clean command.

-d use when no path is specified. So git recurse into untracked directories remove them.

-f/--force To remove nested untracked files.

-i/--interactive Show what would be done and clean files interactively.

-n/--dry-run Show what will happen without removing anything.

-x ignore files

example: git clean -f -d -> Remove all untracked files in current directory any subdirectories.


If you have files you still want to keep:

git clean -di will do an interactive clean which allows you to only delete the files/dirs you don't want anymore.


You might have done a soft reset at some point, you can solve this problem by doing

git add .
git reset --hard HEAD~100
git pull

User interactive approach:

git clean -i -fd

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/amazon/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/s3/ [y/N]? y

-i for interactive
-f for force
-d for directory
-x for ignored files(add if required)

Note: Add -n or --dry-run to just check what it will do.


Remove all things except .git folder and then run git reset --hard


git reset --hard && git clean -dfx

or, zsh provides a 'gpristine' alias:

alias gpristine='git reset --hard && git clean -dfx'

Which is really handy. (warning: The "-x" will also delete 'git ignored' files, so remove this if it is not what you want)

If working on a forked repo, make sure to fetch and reset from the correct repo/branch, for example:

git fetch upstream && git reset --hard upstream/master && git clean -df