Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git reset --hard HEAD leaves untracked files behind

Tags:

git

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

like image 306
Karl Avatar asked Dec 01 '10 18:12

Karl


People also ask

Does git reset hard affect untracked files?

git reset --hard resets your index and reverts the tracked files back to state as they are in HEAD. It leaves untracked files alone.

How do I get my changes back after resetting git hard?

Suppose you staged a file with git add <file-name> and then did a hard reset with git reset --hard HEAD before committing. Afterward, you found out that the staged file is missing. In this case, also, you can recover the files. We can use the command git fsck to recover the files after a hard reset.

Does git reset hard remove added files?

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !


1 Answers

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 
like image 175
knittl Avatar answered Sep 21 '22 15:09

knittl