gitwindows-10recycle-binposh-git

Can 'git clean' move files to trash / recycle bin?


I found git clean -xdf. And I like it, because it cleans up what may IDE created.

Today I was asked by a coworker: Can git clean move files to recycle bin instead of deleting the files?

I checked, git clean deleted some files in a test repository, but didn't move the files to trash.

I know there is option -n for dryrun.

Edit:

I'm on Windows 10 - that's why I have a recycle bin :).
And im using Git from PowerShell with PoSh-Git extension (to get auto completion).


Solution

  • This is pretty fragile, (and isn't git recycling the file) but:

    foreach ($s in git clean -xdn) {Remove-ItemSafely -Force $s.Remove(0,13)}
    

    Caveats:

    1) It'll just delete the file if there's no Recycle Bin for the drive. 2) It'll break if the output of git clean -xdn changes because $s.Remove(0,13) is removing the first 13 characters of each line of output. 3) You may have to run Install-Module Recycle as an administrator on any given machine first.

    A git plugin that returns .NET objects is preferable for something like this. (Because then you could remove-itemsafely $s.FileName, for example)