Does anybody know a way to recursively remove all files in a working copy that are not under version control? (I need this to get more reliable results in my automatic build
Linux command line:
svn status --no-ignore | egrep '^[?I]' | cut -c9- | xargs -d \\n rm -r
Or, if some of your files are owned by root:
svn status --no-ignore | egrep '^[?I]' | cut -c9- | sudo xargs -d \\n rm -r
This is based on Ken's answer. (Ken's answer skips ignored files; my answer deletes them).
If you are using tortoise svn there is a hidden command to do this. Hold shift whilst right clicking on a folder to launch the context menu in windows explorer. You will get a "Delete Unversioned Items" command.
see the bottom of this page for details, or the screen shot below which highlights the extended features with the green stars, and the one of interest with the yellow rectangle...
svn st --no-ignore | grep '^[?I]' | sed 's/^[?I] *//' | xargs -r -d '\n' rm -r
This is a unix shell command to delete all files not under subversion control.
Notes:
st
in svn st
is an build-in alias for status
, i.e. the command is equivalent to svn status
--no-ignore
also includes non-repository files in the status output, otherwise ignores via mechanisms like .cvsignore
etc. - since the goal is to have a clean starting point for builds this switch is a mustgrep
filters the output such that only files unknown to subversion are left - the lines beginning with ?
list files unknown to subversion that would be ignored without the --no-ignore
optionsed
xargs
command is instructed via -r
to not execute rm
, when the argument list would be empty-d '\n'
option tells xargs
to use a newline as delimiter such the command also works for filenames with spacesrm -r
is used in case complete directories (that are not part of the repository) need to be removedI've tried Seth Reno's version from this answer but it didn't worked for me. I've had 8 characters before filename, not 9 used in cut -c9-
.
So this is my version with sed
instead of cut
:
svn status | grep ^\? | sed -e 's/\?\s*//g' | xargs -d \\n rm -r
If you're cool with powershell:
svn status --no-ignore | ?{$_.SubString(0,1).Equals("?")} | foreach { remove-item -Path (join-Path .\ $_.Replace("?","").Trim()) -WhatIf }
Take out the -WhatIf flag to make the command actually perform the deletes. Otherwise it will just output what it would do if run without the -WhatIf.
I ran across this page while looking to do the same thing, though not for an automated build.
After a bit more looking I discovered the 'Extended Context Menu' in TortoiseSVN. Hold down the shift key and right click on the working copy. There are now additional options under the TortoiseSVN menu including 'Delete unversioned items...'.
Though perhaps not applicable for this specific question (i.e. within the context of an automated build), I thought it might be helpful for others looking to do the same thing.