Subversion How To: Replace Trunk or HEAD with an Older Revision

Jan 23 ‘07

Today I was pairing with another developer and we got a little ahead of ourselves and committed some rather drastic changes to the trunk/HEAD of our project. After a few more commits it became obvious that we were stepping on several other developer's toes, so we decided to revert trunk back to the revision just before we nuked the codebase.

Unfortunately, nobody knew how to do this, though it seemed like this should be a pretty common task, since just about everyone has wanted to erase the past for one reason or another. It didn't take much Google'ing to find the answer but the syntax was weird enough that I decided to show it here. Thanks Python Software Foundation's How-To for the tips.

The command:

svn merge -r NEW:OLD PATH

Example:

svn merge -r HEAD:101 http://subversion.example.com/project/trunk

Let's say you committed a real stinker at repository revision 20, and, since you've kept committing, the repository is now at revision 22. Here's how to revert HEAD back to the fresh-smelling revision 19. Here's the story:

[~/40withegg]# svn rm test/
D         test/unit/asset_test.rb
D         test/unit/core_filters_test.rb
D         test/unit/tagging_test.rb
...
D         test

Commit it!

[~/40withegg]# svn ci -m'JLM: deleting tests because I am crazy!'
Committed revision 20.

And, after some other craziness, we committed a few more things:

...
Committed revision 22.

Let's undo all of that. First, revert, just to make sure your working directory is clean:

[~/40withegg]# svn revert *

Next, perform the (horribly named for this purpose) svn merge, which will add the clean revision's code to your working directory as changes:

[~/40withegg]# svn merge -r HEAD:19 svn+ssh://joe@example.com/svn/trunk
A    test
A    test/unit
A    test/unit/user_test.rb
...
A    test/referenced_caching_test_helper.rb

Finally, commit the fix:

[~/40withegg]# svn ci -m"JLM: adding the tests back... man that was dumb."
Adding         test
Adding         test/actor.rb
Adding         test/fixtures
...
Adding         test/unit/user_test.rb
Transmitting file data .
Committed revision 23.
[~/40withegg]#

Hope that helps!

Comments on “Subversion How To: Replace Trunk or HEAD with an Older Revision”

Eric Dugas said...

Thank you sir for taking the time to post this. It really saved the day. I owe you a beer.

Gary said...

Thanks a lot for that useful hint. Saved us a lot of time on or project.

Nicholas Bittencourt said...

Thank you so much!! It saved my day!! :D

Jeff Drost said...

Thank you!!!

Chris Blow said...

Knew I would run into you again Joe -- I guess predictable that it would be through googling for SVN advice ... thanks!!

steven said...

I'm in a dirt, and would like a help!

I will describe the workflow of the donkey:

  • Copy the contents of a local branch to another folder and removed all references .svn this folder.
  • And later removed the contents of the trunk location, via "svn rm" and submitted via commit this change.
  • Copy the content local without the .svn to the trunk local and added to SVN (svn add) and after a commit.

Now SVN does not accept more updates on the svn server stagging production and says that since there is already a copy of the content! (Svn: Failed to add directory '.. \ w +..': object of the same name already exists)

They claimed that the merge was not working because it gave conflicts in all changed files!

How do I restore the remote repository for the review (3243) before these changes??

"svn update -r 3243" + "svn commit", not restory, because it revision exists of already!

Leave a comment

Markdown is allowed. If you’re a robot, don’t even bother.

Click here to go home
Powered by aintablog