1343

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.

Does anybody have an idea how to do that?

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
Aler
  • 15,097
  • 6
  • 23
  • 26

13 Answers13

1954
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)

To see what will be deleted before-hand, without actually deleting it, use the -n flag (this is basically a test-run). When you are ready to actually delete, then remove the -n flag:

git clean -nfd

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
  • 17
    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 07 '09 at 06:49
  • 7
    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 07 '11 at 19:12
  • 10
    That -x switch is unnecessary and somewhat dangerous in this case. – Tim Gautier Jul 26 '11 at 15:48
  • 75
    `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. – Mason Stewart Aug 18 '11 at 01:22
  • 2
    just fyi, if you forget to add the --hard you will need to do a git clean -fxd :) and everything went smoothly after that – agrublev Apr 20 '12 at 22:49
  • 12
    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 02 '13 at 12:31
  • 2
    Maybe this will help someone.. I use the mnemonic git clean -f***ing -die to remember the flags – Nate-Bit Int Dec 17 '14 at 23:16
  • 1
    So that some poor future reader doesn't panic, `git add .` will take about 4 milliseconds to add 3500 files, but `git clean -f(***ing) -d(ie)` takes somewhere between 6 and 8 months. – monsto May 27 '15 at 20:54
  • 1
    This answer should be edited to say that '-d -f' also deletes ignored files. Cost me couple of folders I didn't want to delete. My edit was rejected. – nidalpres Aug 31 '15 at 17:27
  • 5
    You can also use `git clean -n` to see what will happen if you use `git clean -f`. – Horacio Oct 12 '15 at 15:17
  • 1
    Or `git clean -i` which will show you what would be removed with -f, but then let you clean with a keypress, or ask for each, filter, select by number or get help or quit. – Jason Goemaat Dec 31 '15 at 06:58
  • I wish I could give you a +1 for each time I have read this answer... for sure you would have a +20 or +30... – Alfonso Nishikawa Dec 11 '17 at 00:50
  • I was burned by trying `git clean -fxd :/`. DON'T DO THIS UNLESS YOU'RE **VERY** SURE of what *exactly* it will do. You will have a bad time. – naiveai Dec 25 '17 at 03:39
  • git reset --hard does not work for a specific directory. It always affects the entire repo. Not sure if the OP needs that, but others might want it. – donquixote Jan 14 '19 at 18:03
  • Use `git clean -ffxd` to clean submodule folders. – Konard May 14 '20 at 19:23
  • I'm not sure why this answer has so many up-votes considering its dangerous, and I'm not talking about the "git clean" command. The question was how to revert the changes you've made in working directory. The git reset --hard commit deletes an entire commit , which will can cause problems pushing to a remote, since you've deleted an entire commitId from the history. It reads to me like what the OP is asking is how to revert his working directory back to his last commit. – Mauro Torres Feb 08 '23 at 15:16
406

Safest method, which I use frequently:

git clean -fd

Syntax explanation as per /docs/git-clean page:

  • -f (alias: --force). If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.
  • -d. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

As mentioned in the comments, it might be preferable to do a git clean -nd which does a dry run and tells you what would be deleted before actually deleting it.

Link to git clean doc page: https://git-scm.com/docs/git-clean

Community
  • 1
  • 1
Heath Dutton
  • 4,282
  • 1
  • 13
  • 6
  • 145
    I always `git clean -nd .` before actually deleting files using `git clean -fd .` – tbear May 17 '12 at 06:40
  • 14
    Why? Can you explain the detail please. – Greg B Mar 27 '14 at 23:03
  • 33
    Per [git clean](https://www.kernel.org/pub/software/scm/git/docs/git-clean.html#_options) -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
  • 2
    @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 09:19
  • 2
    This removes files, but it does not undo modifications. – donquixote Jan 14 '19 at 18:01
  • 1
    Does not answer the question: how to undo all changes. After running this I still have all of my 'modified' and 'deleted' changes. – bradw2k Feb 24 '19 at 18:06
251

For all tracked 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.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
jfountain
  • 3,715
  • 2
  • 24
  • 22
  • 11
    @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
  • 7
    This is the safest route, but it will not remove untracked files (newly-added files). – TinkerTenorSoftwareGuy Nov 07 '16 at 18:49
  • 10
    This does not restore deleted, yet uncommitted, files. – Cerin Feb 08 '17 at 00:13
  • Just FYI. I missed this. Make sure you are in the directory you want to delete the unstaged changes. For instance, I had changes in 2 different files at different locations on a branch, i.e.: `src/app/work/filename.js` and `../app/working/test/filename.js`. Just using `git checkout -- .` on the branch only deleted the the first file (_without the leading ../_). So I had to `cd ../` up into the second location directory in order to delete the second file using that command. – Chris22 Dec 03 '18 at 20:11
  • 1
    Beginners might find it useful to know what that double dash (--) do there: Although `git checkout file` or `git checkout .` can revert all the changes made to a certain file(in the former one) or changes made to all files(in the latter one), but usually they're not used like that and a double dash precedes them in order to make it completely explicit that it's NOT going to be a "branch checkout". So even if a branch name and a file name are the same, there won't be any name conflict issues. – aderchox Jul 18 '20 at 08:19
  • what does `--` mean? – Robert Jul 20 '20 at 15:14
  • 2
    This didn't work for me. I have unstaged changes (not staged for commit) and yet `git checkout -- .` does nothing. – Wassadamo Dec 19 '21 at 07:25
  • 1
    This did not remove the untracked files for me. – Gustavo Maximo Mar 03 '22 at 20:05
67

You can do this in two steps:

  1. Revert modified files: git checkout -f
  2. Remove untracked files: git clean -fd
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
61

Have a look at the git clean command.

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.

Community
  • 1
  • 1
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • I have tried `git clean -i` and chosen "clean" from menu - this finally removed new files. – Sany May 12 '20 at 10:24
49

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

Community
  • 1
  • 1
donquixote
  • 4,877
  • 3
  • 31
  • 54
  • 1
    Why are you referencing stash@{0} instead of just `git stash drop`? – maikel Nov 06 '14 at 08:47
  • Honestly, I don't remember :) – donquixote Nov 06 '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! – user9869932 Aug 01 '15 at 19:32
  • 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
  • 2
    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 06:59
  • Surely using git stash is the best approach since can recover the data with git stash pop if you later change your mind. – Max888 Jun 13 '22 at 12:28
41

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.

skube
  • 5,867
  • 9
  • 53
  • 77
  • 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
7
git reset --hard origin/{branchName}

It will delete all untracked files.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
santosh kumar
  • 574
  • 8
  • 10
5

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.

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

tg_so
  • 496
  • 6
  • 11
4

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

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

Usage: git discard

Agorreca
  • 684
  • 16
  • 31
3

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.

donquixote
  • 4,877
  • 3
  • 31
  • 54
2

For a specific folder I used:

git checkout -- FolderToClean/*
Glauco Neves
  • 3,483
  • 1
  • 24
  • 36
0

This is probably a noob answer, but: I use TortoiseGit for windows and it has a nice feature called REVERT. So what you do to revert your local nonstaged nonpushed changes is:

  1. bring up a context menu for the needed folder and select revert, it shows a revert popup where you can select changed files to revert/recover.
  2. If you also want to delete added files(that are not in git yet) click commit (from same context menu) it brings up the Commit popup and shows you the added files, then right click each of them and choose delete. But dont press Commit btn in this popup, as you dont wanna commit, but only see added files and delete them from here.