2

Say I crete some files for experimenting without committing them. git status shows:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  file a
  file b

nothing added to commit but untracked files present (use "git add" to track)

If I run git clean -f it will remove files a and b, but also other untracked files, such as node_modules, tmp files etc., which are included under .gitignore. Is there a git command to remove only the untracked files shown under git status or must I remove them manually with rm?

Tim
  • 41,901
  • 18
  • 127
  • 145
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
  • See similar http://stackoverflow.com/questions/61212/removing-untracked-files-from-your-git-working-copy – jayalalk May 08 '14 at 11:48
  • @jayalalk maybe I am missing something, but I don't see the option there to NOT remove ignored files. – Alexander Popov May 08 '14 at 11:51
  • Git ignores stuff in .gitignore... I'll let that sink in for a moment. – anddoutoi May 08 '14 at 11:52
  • Not removing ignored files is the default. The `-x` option also removes ignored files. See `git help clean`. – Leiaz May 08 '14 at 11:53
  • @anddoutoi Not true. In my `.gitignore` file I have `node_modules/*`. After running `git clean -fd` node_modules is gone. – Alexander Popov May 08 '14 at 11:54
  • 3
    It's cause your pattern is wrong. `node_modules/*` ignores everything that is child of `node_modules/`. Use only `node_modules` and will probably do what you want. – anddoutoi May 08 '14 at 11:58

1 Answers1

0

Based on the comments and a related question - by default git clean does not remove ignored files. To include them, one must use the either -x (ignored and non-ignored files) or -X (ignored file only). However, if under .gitignore one specifies a pattern like folder_name/* and then use the -d option, it will also remove folder_name.

Community
  • 1
  • 1
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130