git reset --hard HEAD leaves untracked files behind
GitGit Problem Overview
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"?
Git Solutions
Solution 1 - Git
You have to use git clean -f -d
to get rid of untracked files and directories in your working copy.
You can add -x
to also remove ignored files, more info on that in this excellent SO answer.
If you need to reset an entire repository with submodules to the state on master, run this script:
git fetch origin master
git checkout --force -B master origin/master
git reset --hard
git clean -fdx
git submodule update --init --recursive --force
git submodule foreach git fetch
git submodule foreach git checkout --force -B master origin/master
git submodule foreach git reset --hard
git submodule foreach git clean -fdx
Solution 2 - Git
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.
Solution 3 - Git
git reset --hard && git clean -df
or, zsh provides a 'gpristine' alias:
alias gpristine='git reset --hard && git clean -df'
Which is really handy.
Optional:
There is also an -x
option for the git clean
command. Which will also delete 'git ignored' files, so add this option as well if it is 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
Solution 4 - Git
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"
Solution 5 - Git
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.
Solution 6 - Git
The command you are looking for is git clean
Solution 7 - Git
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.
Solution 8 - Git
You can add this useful alias to hard reset all the files (tracked and untracked) and to come back to the previous commit version:
git config --global alias.reset-hard '!f() { git reset --hard; git clean -df ; }; f'
Then you can reset this way:
git reset-hard
Solution 9 - Git
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