A question I’ve often gotten is “How do I roll back some changes I made in Subversion?” I’m the company expert in Subversion here at mediaRAIN (another one of my hats) so I thought I’d answer the question one more time.
Subversion is great as it allows you to track every change you’ve made and go back to a previous version if needed. Sometimes you only need to go back to an earlier revision temporarily (for example to tag the project at that point). But other times you (or that lousy co-worker who left on vacation yesterday!) have introduced bugs or committed something you shouldn’t have. Now you need to roll back the broken revision(s). Looking at TortoiseSVN’s menu or going through the list of command-line options you’ll see … gasp! … subversion forgot a roll-back feature! How could they do that?! Isn’t that one of the main reasons for using a versioning system like Subversion?
Never fear, Subversion can roll back changes made, it’s just not labeled “Roll Back” and it doesn’t quite undo those changes. See, it is Subversion’s job to track every revision of a document whether good or bad. What if you rolled back the lousy co-worker’s changes and after he gets back from holiday he shoots you to death with his flying-ninja gun because you destroyed all that code he worked on for days? Not good, Subversion needs to remember even the bad times.
So if Subversion won’t go back, it must go forward, and what you really need is the merge feature. Merge let’s you take a collective group of changes between two places (two separate files or two separate revisions, or both) and merge them into your working copy. Why doesn’t it just merge it into the head revision? Well, you’ve got to TEST it first of course! Merge is also used to get changes from a branch to the trunk and visa-versa, so we need to use it the right way for roll-backs.
Because Merge will “apply the differences between two sources to a working copy path.” (command line help `svn help merge`) it takes three parameters: the first source, the second source, and the working copy path. Since you are rolling back changes the first source and second source will be the same path but different revisions. Make sure to use the HEAD revision (or later revision) first and the starting of the bad-news revision second since you are rolling BACK and not forward. Then you’ll set the working copy path (if you’re using TortoiseSVN that will just be the folder you right-clicked on) to the corresponding folder in your working copy.
Command-line Example:
merge svn://domain.com/repo/trunk/folder/changedFile.txt@HEAD svn://domain.com/repo/trunk/folder/changedFile.txt@215 ./folder/changedFile.txt
When using TortoiseSVN you can hit that “Show Log” button to find the revision easily. Command-line users will have to find it first, remember it (yeah, painful ;), and then use it.
You’re not done yet. Now you’ve removed those changes made from your working copy you still need to test your code and the commit the files back to the repository. This will create a new revision that is now fixed, but leaves the broken one in the repo still in case you ever want to “roll back your roll back.”
To prevent your users from using the (sometimes) complicated merge dialog with TSVN:
* show log
* find the revision to roll back
* right-click on that revision
* choose “revert changes from this revision” from the context menu
Nice one Stefan! I’m on Mac all the time now so I didn’t know that option. Tortoise is the one thing I really miss from Windows. [sigh...]
I’m using TortoiseSVN 1.4.8 w/ Subversion 1.4.6. When I right click on the Revision that I want to roll back I get:
Show differences as unified diff
Save revision to…
Open
Open with…
Browse repository
Create tag from revision
Checkout…
Edit author
Edit log message
Copy to clipboard
Search log messages…
Where is “Revert changes from this revision”???
“Revert changes from this revision” is in version 1.5.2
I have version 1.5.2 but I could not see “Revert changes from this revision”
thanks you saved my life ;-)
Very nicely done. This has got to be one of the best explanations of subversion merge operations I have ever ran across. The author even gives the reader context *gasp*.
Well done sir.
I like you explinations. It help me with many problems given other developments. Many thanks for great assistance provided.
Please continue doing this helps for many who are thanking you kindly.
–Bryce
Thank you for a great explanation. It is definitely memorable, especially when you refer to ninjas :)
In T-SVN you can also use the merge dialog and use a revers merge. There you can specify the revs that contain the changes that need to be undone.
Right-click your work folder and select Merge in the T-SVN context menu.
Select the Merge a range of revision option
In the dialog that is displayed:
- Select the trunk in your repository
- Enter the revision (or range of revs) that you want to undo.
- Select the ‘Reverse merge’ option
- Press OK and the changes from the selected revs will be removed in your working copy.
To finalize the operation just commit your working copy to the repository again.
Hopefully this is helpful.
BTW: I’m using TortoiseSVN 1.6.1, Build 16129 – 32 Bit , 2009/04/10 08:14:05
Subversion 1.6.1
Stefan’s post (the first) describes the easiest method I’m aware of, but note that if you don’t see the “revert changes from this revision” option in the context menu, it’s because you need to right click on your working copy file and select show log. If you navigate to showing the log any other way the option isn’t available.
[...] A frantic Google search on roll back and revert revealed that what I needed to do is merge. Jacob Wright goes into detail in his blog How to Roll Back Changes Using Subversion. [...]
SmartSVN is the way to go on a Mac.
Thanks for posting this up on the web. Very helpful =D
This helped me. Thanks for the arg details in the command-line example, specifically the revision syntax ( “@HEAD” and “@XXXX” ).