Undoing changes and new files with Subversion

Update 8 Sept: Ryan Bates’ Railscast episode #36 shows you how to setup a new rails project in Subversion, including setting up trunk/tags/branches, and a number of tips to make using svn with rails easier. He doesn’t cover undoing new files, but it’s a great introduction that I wish I’d had when I started using Subversion with Rails. Highly recommended.


I’m new to subversion, so if there’s a better way to do this, please pass it along. I just worked this out, and I’m posting here as much for my own reference as anyone else’s.

Let’s say you’ve got made a bunch of changes in the working directory of a project managed with Subversion. These changes include new files added to the tree, possibly in new directories/subdirectories. As a concrete example, suppose you’ve just run a Rails generator (e.g. script/generate resource user), and changed your mind (e.g., you should have used scaffold_resource). You want to revert to the last check in, reverting existing files and removing any newly created files and/or directories that don’t exist in the repository. Of course, this whole use case assumes you’ve committed any previous changes before making the changes you don’t like. You do commit incrementally, early, and often, right?

My first instinct was this:

svn revert -R .

But that’s only half the answer. Files already existing the repository are reverted, but new files and directories still abound. A check of svn status reveals the remaining damage:

?      test/unit/user_test.rb
?      test/functional/users_controller_test.rb
?      test/fixtures/users.yml
?      app/helpers/users_helper.rb
?      app/models/user.rb
?      app/controllers/users_controller.rb
?      app/views/users
?      tmp/sessions/ruby_sess.5337b01cd2bbeca8
?      db/schema.rb
?      db/migrate

Assuming you’re on a Unix-like system (oh, say, OS X), the following will remove all of the offending files and directories:

rm -rf `svn st | sed 's/\? *//'`

A check of svn status should now be blessedly quiet.

Obligatory Nag/Warning: Use Caution! Any command line that begins rm -rf is likely to end in tears. Your Mileage May Vary. Caveat Emptor. No Warranty, Expressed or Implied. Want to double check what you’re about to trash? Run the bit inside the backticks (svn st | sed 's/\? *//') to see the file list. Finally, remember to run svn revert -R . first!

Both comments and pings are currently closed.

Comments are closed.