Karl
Karl

Reputation: 14974

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"?

Upvotes: 818

Views: 358048

Answers (10)

Nikola Diklich
Nikola Diklich

Reputation: 516

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"

Compared to git clean (suggested in other answers), the git stash approach has the advantage that your deleted files will now be stored in git, and findable via the reflog, should you discover later that you did not in fact want to delete them. (If the deleted data is huge, this may also be a disadvantage.)

Upvotes: 33

jjnevis
jjnevis

Reputation: 2870

git reset --hard && git clean -df

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. zsh provides a 'gpristine' alias which includes the usage of the -x flag:

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

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

Upvotes: 101

Alin Ciocan
Alin Ciocan

Reputation: 3068

I had a similar issue on Windows and I had to restart PC and make sure I opened the SourceTree/GitBash etc. as an Administrator and then removing the files from interface worked. I assume git clean -f -d it's also going to work if GitBash is opened as Administrator.

Upvotes: -1

Alessandro Argentieri
Alessandro Argentieri

Reputation: 3215

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

Upvotes: 2

knittl
knittl

Reputation: 265141

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

Upvotes: 1255

Yuresh Karunanayake
Yuresh Karunanayake

Reputation: 567

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.

Upvotes: 7

Dexter
Dexter

Reputation: 49

The command you are looking for is git clean

Upvotes: 3

bit_cracker007
bit_cracker007

Reputation: 2569

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.

Upvotes: 22

user3780587
user3780587

Reputation: 65

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

Upvotes: -15

Sogger
Sogger

Reputation: 16122

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.

Upvotes: 76

Related Questions