208

I have an SVN working directory. I made some changes in that directory, and it shows in svn status. But is there any way for me to remove all my changes in there and just get everything from the trunk using the command line?

0

10 Answers 10

299
svn revert -R .
svn up

This will recursively revert the current directory and everything under it and then update to the latest version.

1
  • 2
    Note that this does not work with externals (they are ignored, seemingly).
    – cic
    Jan 16, 2015 at 15:22
89

I used a combination of other peoples' answers to come up with this solution:

Revert normal local SVN changes

svn revert -R .

Remove any other change and supports removing files/folders with spaces, etc.

svn status --no-ignore | grep -E '(^\?)|(^\I)' | sed -e 's/^. *//' | sed -e 's/\(.*\)/"\1"/' | xargs rm -rf

Don't forget to get the latest files from SVN

svn update --force
2
  • Thanks a lot Shaize, this is the most elegant sed, ... pipe I've seen so far for this :) Especially taking care about the ignores also.
    – bully
    Sep 4, 2015 at 14:18
  • Works like a charm. Thanks! Jul 16, 2019 at 6:21
37
svn revert -R .
svn cleanup . --remove-unversioned
2
  • It does not remove *.class files etc. Jul 26, 2018 at 11:45
  • 3
    if you also want to remove ignored files- svn cleanup . --remove-ignored
    – Kip
    Feb 22, 2019 at 13:39
14

If you have no changes, you can always be really thorough and/or lazy and do...

rm -rf *
svn update

But, no really, do not do that unless you are really sure that the nuke-from-space option is what you want!! This has the advantage of also nuking all build cruft, temporary files, and things that SVN ignores.

The more correct solution is to use the revert command:

svn revert -R .

The -R causes subversion to recurse and revert everything in and below the current working directory.

2
  • I often used this trick, but with SVN 1.7, where metadata is stored only in the WC root, this no longer is the clean solution it was. For instance, deleted files remain deleted. May 29, 2013 at 15:41
  • 8
    svn nuke-from-space Oct 21, 2014 at 1:11
9

None of the answers here were quite what I wanted. Here's what I came up with:

# Recursively revert any locally-changed files
svn revert -R .

# Delete any other files in the sandbox (including ignored files),
# being careful to handle files with spaces in the name
svn status --no-ignore | grep '^\?' | \
    perl -ne 'print "$1\n" if $_ =~ /^\S+\s+(.*)$/' | \
    tr '\n' '\0' | xargs -0 rm -rf

Tested on Linux; may work in Cygwin, but relies on (I believe) a GNU-specific extension which allows xargs to split based on '\0' instead of whitespace.

The advantage to the above command is that it does not require any network activity to reset the sandbox. You get exactly what you had before, and you lose all your changes. (disclaimer before someone blames me for this code destroying their work) ;-)

I use this script on a continuous integration system where I want to make sure a clean build is performed after running some tests.

Edit: I'm not sure this works with all versions of Subversion. It's not clear if the svn status command is always formatted consistently. Use at your own risk, as with any command that uses such a blanket rm command.

7

svn revert will undo any local changes you've made

5

You can use the following command to revert all local changes:

svn st -q | awk '{print $2;}' | xargs svn revert
1
  • I get the error message "Not enough arguments" for svn revert
    – circular
    May 14, 2020 at 7:30
4
svn status | grep '^M' | sed -e 's/^.//' | xargs rm

svn update

Will remove any file which has been modified. I seem to remember having trouble with revert when files and directories may have been added.

1

svn revert

0

If you are on windows, the following for loop will revert all uncommitted changes made to your workspace:

for /F "tokens=1,*" %%d in ('svn st') do (
  svn revert "%%e"
)

If you want to remove all uncommitted changes and all unversioned objects, it will require 2 loops:

for /F "tokens=1,*" %%d in ('svn st') do (
  svn revert "%%e"
)
for /F "tokens=1,*" %%d in ('svn st') do (
  svn rm --force "%%e"
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.