Is there a way to recover from an accidental "svn revert"?
Asked Answered
R

11

104

I managed to shoot myself in the foot this morning by doing the following:

  1. Started working on a change to my project
  2. Made a bunch of edits to a bunch of files
  3. Realized that my approach was all wrong, and that I needed to start over
  4. cd'd to the top level of my project and did a "svn --recursive revert ." to restore my local sandbox to its pre-changes state.
  5. Howled in horror as I realized that there had been a number of other changes outstanding in my local sandbox, and I had just obliterated all of them. (the svn server had been down last Friday so I hadn't been able to check them in, and I had forgot about them over the weekend)

Fortunately in this case I had done an "svn diff > temp.txt" before leaving work on Friday, and the temp.txt file was still on my hard drive, so I was able to feed that file into "patch" and recover my lost changes.

But for my future reference (i.e. the next time I make the same dumb mistake)... is there any way to tell svn to undo an "svn revert"? Does svn keep a backup of the local/not-checked-in diffs anywhere?

Resnatron answered 5/10, 2009 at 17:24 Comment(4)
IIRC TortoiseSVN moves the reverted files to the recycle bin, but the original SVN command-line client does not have such nuances.Alarice
Top comment regarding the recycle bin. That's just saved my day!Bunyabunya
mine too! thank you so much. The reverted file just appeared in my recycle bin and could be restored.Debussy
I wish svn had a feature like mercurial's which automatically saves a backup of each file before reverting.Shockproof
B
89

No, (absolutely) NO.

If you say to Subversion it should revert a file, all changes are gone by the wind.

Only your memory can get them back.

Exception: New files you had added, will only lose their status "added", but the file will remain in this directory, only status is unknown("?")

Platform / Software exception: Using TortoiseSVN on Windows, Revert first throws the files into Recycle Bin and then reverts them. You can dig into the Recycle Bin to recover the files.

Bonds answered 5/10, 2009 at 17:36 Comment(16)
Addendum to the above: Version control only helps you if you actually commit the files. If you are in a position that killing your working copy will kill hours of work, then you aren't committing frequently enough.Shellyshelman
Agreed... in this case the svn server was down, and that's why I had un-committed changes still pending. :^(Resnatron
If this happened, you can just file-copy your whole working copy to continue on a different task. In this way your task can still be committed into single commitsBonds
Yes!!! There is a way to recover depending on your O/S and/or IDE -- read the other answers below before you have a heart attack!!Glazunov
If for some reason you cannot commit frequently, consider making patches instead.Crossly
That's not true, its a little bit more complicated than a simple NO. Use a recovery tool or if your lucky the files are in the recycle bin.Smithson
@RalphMclee: It depends on platform and svn client. TortoiseSVN will save reverted new files in recycle bin. This is merely an exception. And I doubt a recovery tool may reconstruct the changes of your files in a consistent manner. It is much easier and clearer to remember: revert will destroy your work, use it carefully!Bonds
This should NOT be marked as the correct answer. The person that responded about the recycle bin is correct.Overstretch
@dcinadr: please note, that recycle bin is a purely tortoiseSVN function. Other SVN clients and non windows platforms are not supporting this behaviour. As the question neither stated client or OS, the answer should not state these as well.Bonds
@PeterParker - but you stated that the answer is "absolutely" NO. That is misleading because the answer is not absolute and there are certain cases (as you stated) where the answer is NOT NO.Overstretch
@dcinad: Not if you are working on (NOT windows) and not if you are using a different SVN Client. Also the "absolutely" is more a didactic advice to see that here is a danger to lose your work. Even if there are exceptions, from didactic POV it is better to remember "Hands off.. it is dangerous"Bonds
@PeterParker - I agree you should never do it. But that said if it does happen I would like to know what my options are. I don't want to be told that there is NO way if there is. For example, I came here looking for a solution for my colleague when he accidentally reverted everything that he was working on for 2 weeks. And your answer was NOT the solution. The solution came from that said the files could be found in the recycle bin. Obviously, most people agree with me since that answer has a higher rating.Overstretch
Saved my day! I accidentally deleted my changes of nearly 300+ lines.. Found it in recycle bin (Windows)Rodrique
The reason I marked this as the answer is because I do not use TortoiseSVN or Windows, so that solution doesn't work for me.Resnatron
If the development is done in Eclipse, it may also be another exception where you CAN restore the previous state before the revert (not to lose your modifications): #16994013Underbelly
Thanks Tortoise to existsShaper
D
110

There is a solution... go to your recycle bin you'll find there the latest version of the deleted file. Tortoise "throwing" to the recycle bin every file that it revert.

Dav answered 5/10, 2009 at 17:24 Comment(6)
Incidentally, this is also why TortoiseSVN seems so slow when doing reverts. Moving a file to the recycle bin is a very slow operation when the recycle bin is full. You can disable this feature in the "General - dialogs 1" TortoiseSVN settings.Rustproof
Thanks man, you saved me! I accidentally did Revert and almost lost 2 weeks of work. Fortunately, I found it in the recycle bin.Lubow
@ArieLivshin, why would you work for 2 weeks without committing? It seems you need to learn more about branches.Bellinzona
Annoyingly ankhsvn doesn't do thisWhitcher
Thank you! Yes this definitely needs to be set as the accepted answer!Biodynamics
Sadly as of 2021 this appears to no longer be the case. It would be great if there was a simple "Are you sure you want to throw away all you local changes" prompt as the menu option is right above the commit option and screwups are easy to make...Wafer
B
89

No, (absolutely) NO.

If you say to Subversion it should revert a file, all changes are gone by the wind.

Only your memory can get them back.

Exception: New files you had added, will only lose their status "added", but the file will remain in this directory, only status is unknown("?")

Platform / Software exception: Using TortoiseSVN on Windows, Revert first throws the files into Recycle Bin and then reverts them. You can dig into the Recycle Bin to recover the files.

Bonds answered 5/10, 2009 at 17:36 Comment(16)
Addendum to the above: Version control only helps you if you actually commit the files. If you are in a position that killing your working copy will kill hours of work, then you aren't committing frequently enough.Shellyshelman
Agreed... in this case the svn server was down, and that's why I had un-committed changes still pending. :^(Resnatron
If this happened, you can just file-copy your whole working copy to continue on a different task. In this way your task can still be committed into single commitsBonds
Yes!!! There is a way to recover depending on your O/S and/or IDE -- read the other answers below before you have a heart attack!!Glazunov
If for some reason you cannot commit frequently, consider making patches instead.Crossly
That's not true, its a little bit more complicated than a simple NO. Use a recovery tool or if your lucky the files are in the recycle bin.Smithson
@RalphMclee: It depends on platform and svn client. TortoiseSVN will save reverted new files in recycle bin. This is merely an exception. And I doubt a recovery tool may reconstruct the changes of your files in a consistent manner. It is much easier and clearer to remember: revert will destroy your work, use it carefully!Bonds
This should NOT be marked as the correct answer. The person that responded about the recycle bin is correct.Overstretch
@dcinadr: please note, that recycle bin is a purely tortoiseSVN function. Other SVN clients and non windows platforms are not supporting this behaviour. As the question neither stated client or OS, the answer should not state these as well.Bonds
@PeterParker - but you stated that the answer is "absolutely" NO. That is misleading because the answer is not absolute and there are certain cases (as you stated) where the answer is NOT NO.Overstretch
@dcinad: Not if you are working on (NOT windows) and not if you are using a different SVN Client. Also the "absolutely" is more a didactic advice to see that here is a danger to lose your work. Even if there are exceptions, from didactic POV it is better to remember "Hands off.. it is dangerous"Bonds
@PeterParker - I agree you should never do it. But that said if it does happen I would like to know what my options are. I don't want to be told that there is NO way if there is. For example, I came here looking for a solution for my colleague when he accidentally reverted everything that he was working on for 2 weeks. And your answer was NOT the solution. The solution came from that said the files could be found in the recycle bin. Obviously, most people agree with me since that answer has a higher rating.Overstretch
Saved my day! I accidentally deleted my changes of nearly 300+ lines.. Found it in recycle bin (Windows)Rodrique
The reason I marked this as the answer is because I do not use TortoiseSVN or Windows, so that solution doesn't work for me.Resnatron
If the development is done in Eclipse, it may also be another exception where you CAN restore the previous state before the revert (not to lose your modifications): #16994013Underbelly
Thanks Tortoise to existsShaper
N
31

Not really Subversion specific, but if you're working with Eclipse, you can try your luck in the local history.

Now, something a little bit more Subversion specific: if you don't want to make a branch for every change you do, you can keep a couple of trunk checked out locally (trunk-modif-1, trunk-modif-2...). Each "modification" is done on a separate tree and you only need to keep a list of which check out correspond to which modification.

Or you could use Git locally but I never tried it.

Nonferrous answered 5/10, 2009 at 22:22 Comment(2)
FYI - get to the local history via the right click context menu "Restore from local history".Glazunov
For the Eclipse part, here is a more concrete answer: https://mcmap.net/q/205967/-eclipse-reverts-all-changes-to-file-on-key-press-undo-not-possibleUnderbelly
T
14

Recently did this mistake of not committing new file changes (about 10) onto SVN and they all vanished because of my silly mistake. But what saved me was the windows "Restore previous versions" option in the context menu. Phew that was a relief and lesson learnt.

Tait answered 17/11, 2011 at 10:12 Comment(3)
Thank you! This saved me from redoing hours of work! I've never seen this feature of windows before, but it saved my bacon!Sheryl
Where can I send the $6000 I owe you for this having thus saved days of work? Lesson learned...Glazunov
I keep the checkpointing supporting such a restore turned off because it ruins performance. Recently, Eclipse deleted a folder of my projects including its git archive, due to a problem with its Undo feature, and I rather wished that I had had it turned on. However, a Windows program called Recuva saved me by searching the deleted file space. I was very careful not to add additional content to the affected drive until I had recovered all of my files, and it gave me every source file changed since my most recent backup a week earlier. Binary files were mostly not recoverable.Dodecagon
F
8

You can't recover the files, if you are using svn command-line client.

But you can recover them, if you are using TortoiseSVN as your svn client. TortoiseSVN will automatically move them to your Recycle Bin. You can restore them from there after an accidental revert.

This is a configurable option in TortoiseSVN. It is under Settings -> Dialog 1 -> Use recycle bin when reverting.

By default it is checked, which means files are moved to recycle bin. If you want(mostly you don't care to disable this. If you have a reason, please let me also know)

Franz answered 14/9, 2015 at 19:45 Comment(2)
Awesome!... saved meBookseller
Sadly as of 2021 this appears to no longer be the case. (while the settings option is there, the previously changed local files didn't go to the recycle bin) It would be great if there was a simple "Are you sure you want to throw away all you local changes" prompt as the menu option is right above the commit option and screwups are easy to make...Wafer
D
6

If you are using InteliJ then you are a lucky person. On the top menu, you have a Version Control option, and under it you will find local history option, where you will find all the history for selected file, including all the operation you did with that file (update,commit, revert ).

Good luck, Arkde

Determinism answered 2/3, 2012 at 9:0 Comment(1)
And if it was a new file, the file itself will get deleted. In that case select the folder the file was in from Project view and then select 'Local History'. IntelliJ saved the day again.Scheld
C
4

Also if you have reverted your .NET code (.cs files etc) and you've built your application before revert but after you've made changes, you could recover changes from assembly file using any of the reflector tools to view code.

Checky answered 17/9, 2012 at 15:48 Comment(0)
W
3

I was extremely lucky to find a local change log of the file (Local Revisions) in the history browser in Eclipse. I had been inspecting the differences one by one and I had made a couple of saves on the file before overwriting.

Widely answered 8/2, 2018 at 15:39 Comment(0)
P
2

I reverted solution (Visual Studio + AnkhSVN ) and lose changes of few files. I can't find the file in the recycle bin.

BUT: I recovered my losing files with Handy Recovery, i.e. SVN just delete my files on reverting.

Use any data recovery software to recovery reverted files(like trial Handy Recovery)

Protactinium answered 24/12, 2014 at 15:5 Comment(0)
C
1

No, SVN does not keep a log or backup (although there is a solution for TortoiseSVN noted in the other answers).

But in case someone is browsing the answers and did not notice this comment in the OP's question:

"Fortunately in this case I had done an "svn diff > temp.txt" before leaving work on Friday, and the temp.txt file was still on my hard drive, so I was able to feed that file into "patch" and recover my lost changes."

I imagine it is a common paradigm to do 'svn diff' BEFORE you do an 'svn revert'. If you get in the habit of doing this, even if the diff is just echoed to the terminal, you've at least got one possible avenue to recover your lost changes.

Camala answered 9/12, 2016 at 16:53 Comment(0)
T
0

As of 2024, if you use TortoiseSVN like me, just open the Show log option to see the history of the folder contains the file you deleted. Then you can copy the source code of the deleted file. I accidentally moved the file to a new folder than reverted the changes. The file was deleted with the new folder. By doing this, I got the source code again. See more at tortoise website

Open show log option by right-clicking the file directory in window explorer

Revision log dialog

Typewriting answered 4/1 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.