git: undo all working dir changes including new files

The name of the picture


git: undo all working dir changes including new files



How to delete all changes from working directory including new untracked files. I know that git checkout -f does that, but it doesn't delete new untracked files created since last commit.


git checkout -f



Does anybody have an idea how to do that?





@Joel & Wayfarer: why don't you try git help reset and git help clean
– SHernandez
Jan 4 '15 at 16:53


git help reset


git help clean





Making an alias for this in your git config is a good idea ;)
– anubina
Sep 9 '15 at 17:02




12 Answers
12


git reset --hard # removes staged and working directory changes

## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)





Note: git reset --hard removes staged changes as well as working directory changes. Also, git clean -f -d is probably a better opposite of adding a new untracked file. From the question, the asker may be quite happy with his current set of ignored files.
– CB Bailey
Jul 7 '09 at 6:49





Read the next answer and watch out of the -x switch. (It might also remove your local config such as password/db-settings files. e.g. database.yml)
– Boris
Apr 7 '11 at 19:12







That -x switch is unnecessary and somewhat dangerous in this case.
– Tim Gautier
Jul 26 '11 at 15:48





git clean -fxd can actually be REALLY dangerous if you don't know what you're doing. You may end up permanently deleting some very important untracked files, such as your database, etc. Use caution.
– Masondesu
Aug 18 '11 at 1:22


git clean -fxd





Note that git clean -f -d will delete files from ignored folders too. So all you local logs and things like that will be gone. Usually it's not a big problem, but it's better to know.
– cyriel
Oct 2 '13 at 12:31


git clean -f -d



Safest method, which I use frequently:


git clean -fd





I always git clean -nd . before actually deleting files using git clean -fd .
– tbear
May 17 '12 at 6:40


git clean -nd .


git clean -fd .





Why? Can you explain the detail please.
– Greg B
Mar 27 '14 at 23:03





Per git clean -n option is actually a dry run which doesn't remove anything, it just shows you what will be done.
– RNickMcCandless
Apr 30 '14 at 15:45





@tbear, You can always do a blank add -A + commit -a + revert head first before git clean. Reviewing each and every delete simply doesn't scale for major scenarios. Also, dry running is not a silver bullet: what if you missed something or made a mistake during the review?
– Pacerier
Oct 20 '15 at 9:19




add -A


commit -a


revert head


git clean



For all unstaged files use:


git checkout -- .



The . at the end is important.


.



You can replace . with a sub-directory name to clear only a specific sub-directory of your project. The problem is addressed specifically here.


.





@Vincent: The -- avoids typing errors by telling the checkout command that no more parameters are specified. Without them you could end with a new branch instead of reseting the current one!
– Igor Rodriguez
Feb 13 '15 at 14:58





This is the safest route, but it will not remove untracked files (newly-added files).
– TinkerTenorSoftwareGuy
Nov 7 '16 at 18:49





git checkout . also works~
– normalUser
Nov 11 '16 at 14:34





This does not restore deleted, yet uncommitted, files.
– Cerin
Feb 8 '17 at 0:13



Have a look at the git clean command.


git clean



git-clean - Remove untracked files from the working tree



Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.



Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.



The following works:


git add -A .
git stash
git stash drop stash@{0}



Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.



A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.



Credits: https://stackoverflow.com/a/52719/246724





Why are you referencing stash@{0} instead of just git stash drop?
– maikel
Nov 6 '14 at 8:47


git stash drop





Honestly, I don't remember :)
– donquixote
Nov 6 '14 at 10:18





This one worked. As a note, ensure you are in the home directory ( git add -A . ). I have lost 30m becase there was no file match. Thanks!
– xyz
Aug 1 '15 at 19:32


git add -A .





I used to use this approach, and it has merit. However, I have since found the solutions proposed by jfountain and Heath Dutton more apt to the original problem, IMHO.
– HeyZiko
Mar 15 '16 at 18:55





git stash drop stash@{0} drops latest stash if you did git stash it would drop all stashes you did
– DonatasD
Aug 30 '16 at 6:59



You can do this in two steps:


git checkout -f


git clean -fd



I thought it was (warning: following will wipe out everything)


$ git reset --hard HEAD
$ git clean -fd



The reset to undo changes. The clean to remove any untracked files and directories.


reset


clean





This doesn't work, I still get a conflict warning when doing a git pull afterwards: CONFLICT (content): Merge conflict in... I only solved this by a git pull --strategy=ours
– rubo77
Nov 15 '16 at 15:57


git pull


CONFLICT (content): Merge conflict in...


git pull --strategy=ours


git reset --hard origin/{branchName}



It will delete all untracked files.



An alternative solution is to commit the changes, and then get rid of those commits. This does not have an immediate benefit at first, but it opens up the possibility to commit in chunks, and to create a git tag for backup.



You can do it on the current branch, like this:


git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git revert HEAD
git reset HEAD^^



Or you can do it on detached HEAD. (assuming you start on BRANCHNAME branch):


git checkout --detach HEAD
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git checkout BRANCHNAME



However, what I usually do is to commit in chunks, then name some or all commits as "DISCARD: ...". Then use interactive rebase to remove the bad commits and keep the good ones.


git add -p # Add changes in chunks.
git commit -m"DISCARD: Some temporary changes for debugging"
git add -p # Add more stuff.
git commit -m"Docblock improvements"
git tag archive/local-changes-2015-08-01
git rebase -i (commit id) # rebase on the commit id before the changes.
# Remove the commits that say "DISCARD".



This is more verbose, but it allows to review exactly which changes you want to discard.



The git lol and git lola shortcuts have been very helpful with this workflow.


git lol


git lola



git clean -i will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.


git clean -i



See git help clean for more information, including some other useful options.


git help clean



If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig. For instance:


.gitconfig


[alias]
discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"



Usage: git discard


git discard



For a specific folder I used:


git checkout -- FolderToClean/*





That doesn't delete untracked files, as OP states.
– vonbrand
Aug 26 '15 at 23:57






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions