65

Possible Duplicate:
How do you remove untracked files from your git working copy?

Is it possible to tell git to remove untracked files? Mainly something that is similar to a reset?

example:

git checkout -- index.php <-- revert my file
git checkout -- master <-- this would revert the entire repo back to the last commit on master, removing (deleting) any and all untracked files as well as reverting committed ones.

I know this is trivial to do on the shell. But I'd like to know if this can be done in Git?

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
zkolnik
  • 882
  • 2
  • 7
  • 11
  • 1
    `git checkout -- master` will **not** check out branch `master`, but will check out the **file** called `master` at the current commit (`HEAD`) – knittl Sep 28 '11 at 19:39

2 Answers2

65

You need git clean but add the -df to enable removing files that are in directories from where you are. Add x to include ignored files.

So to completely clean your working directory leaving only what is in source control, issue this command:

git clean -xdf
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • upvote indeed. http://git-scm.com/docs/git-clean – gp. Dec 23 '13 at 10:48
  • 2
    This is the ultimate answer to the question. Accepted answer clears the inside of an untracked directory but leaves the directory's itself. – amertkara Jun 03 '15 at 12:03
61

You may be looking for git clean. This will delete all untracked files. By default this ignores (does not delete) patterns in .gitignore, but git clean -x cleans those files too.

From the git clean man page:

   -x
       Don't use the ignore rules. This allows removing all untracked
       files, including build products. This can be used (possibly in
       conjunction with git reset) to create a pristine working directory
       to test a clean build.
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 3
    I have to use `-f` instead. `-x` seems to not work for me, got this error: `fatal: clean.requireForce defaults to true and neither -i, -n nor -f given; refusing to clean` – Hlung Jun 19 '14 at 05:12
  • 3
    @Hlung: It looks like the default for your Git installation is to require `-f`. So you would use `git clean -f -x`. – Greg Hewgill Jun 19 '14 at 07:18