0

I was trying to perform git clean for some untracked files. I typed the following, unintentionally right, command:

git clean -dn | grep -v <files_to_exclude> | git clean -df

Note that the line seems to be missing a xargs. That is, I'd have normally written the previous command like this:

git clean -df | grep -v <files_to_exclude> | xargs git clean -df --

That being said, the former worked and the latter didn't! Yes, I know I could have just used:

git clean -df --exclude=<files_to_exclude>

I didn't know about the exclude option back then.

Just to make sure you have the right picture, let's say you have three untracked files x, y, and z and you are to exclude x.

$ git clean -dn | grep -v x
Would remove y
Would remove z

It makes sense the plumbing this output to xargs directly without omitting "Would remove " part is wrong and will cause git clean to break.

Now the question is: why did it work with plumbing this output directly to git clean it still worked?

joker
  • 3,416
  • 2
  • 34
  • 37

1 Answers1

0
  • "the former worked" : it behaved exactly as git clean -df <no extra args>, which cleans all the files, not just the ones you wanted to include ...
  • "the latter didn't" : [edit] running it on my machine, "it works" (with the big caveat that grep will actually match full messages instead of file names, so running grep -v Would for example excludes all lines, and if the output is empty, you fall back on git clean -df which once again removes all instead of nothing ....)
    what error do you see in your case ?
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • You're correct. git clean worked as if there was no piped input and cleaned all files. So, this means git clean when it doesn't understand the input, it behaves as if there isn't any? – joker Aug 05 '20 at 12:48
  • 1
    some commands never read their stdin. `echo foo | echo bar` will output `bar`, as expected, and never reads its stdin – LeGEC Aug 05 '20 at 12:49
  • Gotcha. You're right again. My fault missing this out. – joker Aug 05 '20 at 12:50
  • 1
    @joker: extra emphasis on the "empty stdin" part : if stdin is empty, running `... | grep -v . | xargs git clean -fd --` is equivalent to `git clean -fd `. – LeGEC Aug 05 '20 at 12:53
  • If you happen to have the `xargs ...` variant in one of your scripts, make sure to fix the bug ... – LeGEC Aug 05 '20 at 12:54
  • Yes, I should have used `sed`, `awk`, or `cut`. I also understand your emphasis on empty filtering will cause git clean to clean all. Your help is much appreciated. – joker Aug 05 '20 at 12:58