Sometimes svn switch
fails because unversion files exist in the working copy, and the need to erase them comes up. This is also true when updating to an old revision (usually one that misses directories that exist in the head revision), doing some work (like compiling), and then trying to update back to the head revision. Subversion (SVN) will fail complaining that directories/files already exist.
The cause of the problem is that the working-copy isn’t clean, it has some (or a lot) of unversioned files. The best solution will be to delete all of them. While this can be a tedious task when done manually for each file, it can be achieved with single shell command.
svn status --no-ignore | grep '^\?' | sed 's/^\? //'
svn status --no-ignore | grep '^\?' | sed 's/^\? //' | xargs -Ixx rm -rf xx
The first command will list all unversioned files and directories. Use this to review the files to be deleted. After you review the files, use the second command to delete them easily.
UPDATE 2010-12-29: Fixed the command, so it will handle spaces in filenames correctly.
Thanks!
jeez men, thanks !
sweet. you win one free internets
Thanks man. Helped alot
Good.. Thanks a lot 🙂
You can do a similar thing on Windows using PowerShell:
(svn stat “–no-ignore”) -match ‘^[I?]’ -replace ‘^.\s+’,” | rm
RE: Damian Powell
Tried (svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm
but it didn’t quite work for me
(svn status –no-ignore) -match ‘^[?]’ -replace ‘^.\s+’ | rm
works fine for me though.
http://pastebin.ca/1494457
*sighs* the blog removes some formatting, does it…
Yes it does, and unfortunately I still don’t know of a way around it in the comments that works perfectly. However in most occasions putting the code inside pre tags (and setting
lang
attribute to whatever you want) gives you preformatted syntax highlighted code. For example:Don’t forget to escape those spaces!
You can do it with sed only:
svn status –no-ignore | sed -n ‘s/^\?\s*\(.*\)/\1/p’ | xargs rm -rf
To properly deal with spaces one has to do:
svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/\([^^]\) /\1\\ /g' | xargs rm
in mac osx at least…
scratch my last comment, it’s Ivan’s way and should read
svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/ /\\ /g' | xargs rm
or something like that… Jeez this isn’t easy…
How about just cutting the 8 first characters?
@Lionel, that indeed seems to be a very nice solution. But I think it should be like this
I’ve replaced the 8 with a 9 as you want to start printing from the 9th byte to the eol.
why not just using awk?
svn status –no-ignore|awk ‘/^\?/ {print $2}’|xargs rm
awk
is a great tool, but I lack comprehensive knowledge of it, thus I usually prefer other tools.Piping lines into xargs rm here *will* give you grief if you have spaces in your file names!
This is safer:
(svn status | grep ‘^[\?~]’| cut -c 9- )| while read VICTIM; do
rm -fr “$VICTIM”
echo “Deleted $VICTIM”
done
Great! Thanks a lot!
Late to the party but… Andy, you’re right that spaces in filenames will cause problems if you pass them to xargs, but they’re not the only problematic character. The usual way to fix this is to use tr and the -0 flag on xargs, eg:
ls|tr ‘\n’ ”|xargs -0 rm
It’s what -0 is there for.
Ugh, the blog mangled my command. The tr command was
tr space single-quote backslash n single-quote space single-quote backslash zero single-quote
Thanks,
Worked out of the box…..
thank u very much man
Thank you very much for this. I’ve been doing it manually, one file at a time for a very long time now and it only just occurred to me to search for an automated solution.
Seriously, you have saved me so much time. Thank you.