Delete Unversioned Files Under SVN
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!
gilesc
9 Feb 08 at 08:46
jeez men, thanks !
gibffe
15 Aug 08 at 15:26
sweet. you win one free internets
DWC
1 Apr 09 at 18:38
Thanks man. Helped alot
Ettiene
4 May 09 at 15:06
Good.. Thanks a lot
sridharkannan
24 Jun 09 at 17:14
You can do a similar thing on Windows using PowerShell:
(svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm
Damian Powell
4 Jul 09 at 15:47
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.
Yumi Nanako
14 Jul 09 at 08:58
http://pastebin.ca/1494457
*sighs* the blog removes some formatting, does it…
Yumi Nanako
14 Jul 09 at 11:18
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
langattribute to whatever you want) gives you preformatted syntax highlighted code. For example:Guy
16 Jul 09 at 06:56
Don’t forget to escape those spaces!
Ivan
23 Jul 09 at 14:32
You can do it with sed only:
svn status –no-ignore | sed -n ‘s/^\?\s*\(.*\)/\1/p’ | xargs rm -rf
DEBO Jurgen
12 Nov 09 at 23:37
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…
joao
25 Dec 09 at 19:09
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…
joao
25 Dec 09 at 19:43
How about just cutting the 8 first characters?
Lionel
1 Feb 10 at 10:51
@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.
Guy
1 Feb 10 at 21:28
why not just using awk?
svn status –no-ignore|awk ‘/^\?/ {print $2}’|xargs rm
tobi
3 Mar 10 at 14:37
awkis a great tool, but I lack comprehensive knowledge of it, thus I usually prefer other tools.Guy
4 Mar 10 at 19:05
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
Andy
18 Jun 10 at 18:19
[...] to Guy Rutenberg for this, your definitely in my list of top 5 [...]
Optimus Pete - svn: delete unversioned files
9 Jul 10 at 18:38
Great! Thanks a lot!
Alexander M. Batishchev
18 Jul 10 at 20:12
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.
Baz
28 Oct 10 at 15:19
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
Baz
28 Oct 10 at 15:21
Thanks,
Worked out of the box…..
Japan Shah
6 Apr 11 at 13:11
[...] Delete Unversioned Files Under SVN by Guy Rutenberg [...]
Delete Unversioned Files Under SVN by Guy Rutenberg – Code Cafe
30 May 11 at 11:44
thank u very much man
Harsh kumar
6 Sep 11 at 13:35
[...] texto foi adaptado do link http://www.guyrutenberg.com/2008/01/18/delete-unversioned-files-under-svn/ Leave a [...]
Fabio Brandão – Blog » Blog Archive » Excluindo arquivos não versionados no SVN
23 May 12 at 19:58
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.
Josh Greco
25 Sep 12 at 17:00