How do I return to an older version of our code in Subversion?
I'm working on a project with a friend and I want to return to an older version of our code and set it to be the current. How do I do it?
I'm using "anksvn" on vs08.
I have the version that I want on my PC, but the commit is failing; The message that I get is "commit is failed, file or directory is out of date."
I also have the subversion client on my PC.
Basically you need to "merge backwards" - apply a diff between the current and previous version to the current version (so you end up with a working copy looking like the old version) and then commit again. So for example to go from revision 150 (current) back to revision 140:
svn update
svn merge -r 150:140 .
svn commit -m "Rolled back to r140"
The Subversion Red Book has a good section about this.
You can only commit new changes at the head of the subversion history.
The reason you can't do anything directly with the good copy you have on your PC, is that its .svn
folders know that it is code from the past, so requires an update before any commit.
Find the good revision number and revert
-
Find the revision number of the old copy you want.
Get your current revision with:
svn info --show-item revision # or svn log
Or to check older versions of your project, use:
svn update -r <earlier_revision_number>
until you find the right revision number.
-
Note down the good revision number (assuming
123
for examples below). -
Update to the latest revision:
svn update
-
Undo all the changes between the revision you want and the latest version:
svn merge -r HEAD:123 . svn commit -m "Reverted to revision 123"
(the same as Jon Skeet's answer above.)
If you can't find the revision number
If you can't find the old copy, and you just want to commit the files currently on your PC:
-
Make a copy of your good version (but without any
.svn
folders):cd .. rsync -ai --exclude=.svn project/ project-good/
-
Now make sure you have the latest version:
cd project svn update # or make a fresh checkout svn checkout <url>
-
Copy your good version over the top of the working copy.
This command will copy, and also delete any files from the working tree that aren't in your good copy, but it won't affect the existing
.svn
folders.cd .. rsync -ai --exclude=.svn --delete project-good/ project/
If you don't have rsync, you can use
cp -a
but you will also need to delete any unwanted files manually. -
You should be able to commit what you have now.
cd project svn commit -m "Reverted to good copy"
Just use this line
svn update -r yourOldRevesion
You can know your current revision by using:
svn info
The standard way of using merge to undo the entire check-in works great, if that's what you want to do. Sometimes, though, all you want to do is revert a single file. There's no legitimate way to do that, but there is a hack:
- Find the version that you want using svn log.
-
Use svn's export subcommand:
svn export http://url-to-your-file@123 /tmp/filename
(Where 123 is the revision number for a good version of the file.) Then either move or copy that single file to overwrite the old one. Check in the modified file and you are done.