40

I'm using Git for version control. Now I want to delete on my local machine all files in the repository that are not tracked.

I can use

git status

to list these files, but how can I delete all of them?

  • 1
    Cant you just check out all the files to a different place one your machine and then delete the old version? – Nifle Sep 14 '11 at 13:59
  • 1
    For sure I can, but isn't there a simpler way? – RoflcoptrException Sep 14 '11 at 14:01
56

If you have it in ignore, use git clean -xf. You can do git clean -df but that will also remove un-tracked directories. Use -n for a dry-run.

See cleaning up un-tracked

  • 3
    -x ignores the ignores.. – UpAndAdam Apr 10 '14 at 19:01
  • 8
    -xf will delete and the ignored files and this is probably NOT what you want. Use -nf and proceed with care. See: stackoverflow.com/questions/61212/… – Mr_and_Mrs_D Jul 9 '14 at 21:13
  • 2
    Remember, this may remove your environment/IDE folders (e.g. .vagrant or .idea (phpstorm)) – timhc22 Nov 21 '14 at 12:12
  • 1
    WATCHOUT! gitignored files will be purged forever. -100 – sepehr Feb 26 '19 at 14:45
5
git clean -f
  • 8
    It might make sense to add the meaning of that command to make this answer self-contained. – N.N. Sep 14 '11 at 18:47
5

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 file
-d for directory

Note: Add -n or --dry-run to just check what it will do.

3

There is a subtlety worth mentioning about git clean -f vis-a-vis untracked files and directories. If you have an untracked directory that contains files which are, a fortiori, untracked, then git clean -f will NOT delete those untracked files.

In other words, it is NOT always the case that git clean -f will delete all untracked files. A better explanation of git clean -f is that it deletes all untracked files that are not in untracked directories.

git clean -f -d must be used to delete untracked files that are in untracked directories and there appears to be no way to delete all untracked files without also deleting untracked directories that only contain untracked files.

Use git clean -f -d -n to REALLY see what you want to do to restore your working directory to what it would be without any untracked files. Then use git clean -f -d to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.