git reset --hard HEAD leaves untracked files behind
Asked Answered
P

10

791

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

Polymerize answered 1/12, 2010 at 18:26 Comment(3)
git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.Tannen
@Tannen -- correct. This was stated already in the question.Crumble
I noticed that you need have all the unstaged files in your path, not some directories above like ../../file.yaml. Instead you need to first cd ../../ then do git reset --hard and/or git clean -fd to reset everything. When you run git status nothing should be any directories above you. An easy way is to just do these commands from the base directory of the repo.Forecourse
O
1222

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
Overprize answered 1/12, 2010 at 18:28 Comment(11)
Also -x if you want to remove your .gitignored files and get back to a pristine state.Secretariat
Add -n to test would be removed first. combine all of them in one argument: -dfnHolster
My common command is git clean -qfdx here. Remove everything and do it silently.Paraffin
-d -f can be decalred twice -dff for -d -f -f, this will delete ALL untracked directories, including protected untracked directories.Pedestrian
wait so this deletes files? What if you want the untracked files to be exactly as everything was from before?Slingshot
@BKSpurgeon: yes, it deletes files. What do you mean with »I want untracked files as they were before«? Git doesn't know anything about untracked files, except that they exist. It doesn't track multiple versions of these files (since they are untracked).Overprize
@Overprize - of course that makes perfect sense! i'm inexperienced with git hence the sillyness of my question. thx for the clarification. +1.Slingshot
see How do I remove local (untracked) files from my current Git branch?Supplication
there's a lot of duplication in that script. But instead of listing it, lemme contribute an edit...Scapegrace
doesn't work for me, neither any command, even in comments. files are still in index and its written as "modified"...Evadne
thanks. git clean -f -d followed by git reset --hard origin/master worked for meHillel
Y
95
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
Yuk answered 9/7, 2015 at 15:29 Comment(10)
Apologies if this is not a safe command - I was not trying to be safe, I was trying to answer the question. Could you comment on whether this answers the question?Yuk
This works well and should be built into git IMHO (although I'm not sure I would use -x routinely). So many times I'm working on a local project, not yet synced to github etc, and a messy refactor goes haywire beyond the IDE 'undo' state. My instinct is to revert to last commit but googling for that usually takes to answers for penultimate commit, not last commit. All I want to to is get back to most recent commit. This does that. Should be an easier way though. Thanks Linus! ;-)Decolorant
It's dangerous because it also deletes ignored files with the -x like if you'd have just cloned the repo. If that's what you want, it's perfect. If you just want to delete untracked files, removing the -x option works well.Strophanthus
Thank god for gpristineChukchi
and deletes intellij settings ;)Acton
Whoever comes to this answer, DO NOT EXECUTE git clean -dfx. This will delete files ignored as well. It's not safe to execute this command, except you know what you're doingBreen
I updated the answer to NOT include the -x option by default. Since this is a place where most people copy/paste. It was VERY dangerous.Coombs
This answer is wrong and dangerous. The gpristine alias is not as listed here - IT ALSO DELETES IGNORED FILES (it includes the x flag) Please update your answer.Charwoman
today I learn git clean -dfBlackford
Updated the answer once again, because the update removing the-x option from the top script also mistakenly removed it from the gpristine alias, however gpristine DOES INCLUDE THAT OPTION (and therefore it's unsafe - learned it the hard way)Interphone
R
76

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.

Roomette answered 23/6, 2014 at 16:55 Comment(0)
C
28

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"
Colatitude answered 8/8, 2019 at 11:36 Comment(2)
Simple and more intuitive than the git clean options.Tequilater
The alias is nice addition to it! Thanks, will be using it from now on.Paludal
A
22

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.

Arrington answered 14/6, 2017 at 4:7 Comment(0)
H
5

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.

Habitable answered 3/12, 2019 at 9:58 Comment(0)
N
3

The command you are looking for is git clean

Nosography answered 2/12, 2017 at 20:10 Comment(1)
Next time please do add a little more description/examples etc. Whatever helps the user to understand what it does, and, in this case, what parameters to use.Navel
U
1

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
Uniliteral answered 20/12, 2021 at 8:56 Comment(0)
I
0

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.

Immoderacy answered 15/2, 2023 at 17:2 Comment(0)
H
-15

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
Hoopoe answered 26/6, 2014 at 18:34 Comment(5)
I don't think this is what OP wanted. Either of the other answers do a much better job of actually showing how to fix this.Abiotic
This can also be slow if you have a lot of files to add.Blight
totally unrelated.Eyeopener
Doing this, the untracked files are removed. But as programmers, everyone should try to find correct solution for problems without using workarounds and smile.Habitable
Guys, this answer is not THAT (-16) bad. It solves OP's problem - a clean state. Git beginners can understand these commands. SO is full of workarounds, take this just as an option.Gastrotrich

© 2022 - 2024 — McMap. All rights reserved.