Search code examples
gitchaininggit-cleangit-plumbing

Chaining git clean (Porcelain) command


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?


Solution

    • "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 ?