125

I deleted a file from a repo and now want to put it back. The best I can figure out is to:

  • update to the revision before the delete
  • copy the files elsewhere
  • update to head
  • copy the files back
  • add them
  • commit

That just smells bad and it looses all history to boot. There has got to be a better way to do this. I have already looked in The SVN Book but didn't find anything and am now looking down the SVN tag list.

leander
  • 8,527
  • 1
  • 30
  • 43
BCS
  • 75,627
  • 68
  • 187
  • 294

9 Answers9

185

The problem with doing an svn merge as suggested by Sean Bright is that is reintroduces other changes made in the same revision as the deletion. An svn copy is a more targeted operation that will only affect the deleted files.

Using Tortoise SVN you can resurrect a file that has been deleted from your working copy directory and from later SVN revisions, via a svn copy as follows:

  • Browse to the working copy folder that previously contained the file.
  • Right click on the folder in Explorer, go to TortoiseSVN -> Show log.
  • Right click on the revision number just prior to the revision that deleted the file and select "Browse repository".
  • Right click on the deleted file and select "Copy to working copy..." and save.

The deleted file will now be in the working copy folder. To re-add it back to SVN, right click on the restored file and select SVN Commit.

NB: This method will preserve the previous history of the restored file, however to see the prior history in the TortoiseSVN log you need to make sure "Stop on copy/rename" is unchecked in the Log messages dialog.

tekumara
  • 8,357
  • 10
  • 57
  • 69
  • 27
    This can also be performed directly on the server, useful if the deleted files or folder were quite large: `svn cp -r 1993 http://example.com/svn/trunk/path@1993 http://example.com/svn/trunk/path` – Craig Oct 24 '11 at 08:05
  • This is the right way to do it. Just tried with TortoiseSVN 1.8.1 and it works as advised. – Dr. Gianluigi Zane Zanettini Sep 03 '13 at 06:47
  • Perfect!! Simple and effective. Thanks for the solution – Hunter Sep 06 '13 at 09:47
  • This method made it much easier for me to restore a file into a child branch which had been deleted from its parent branch. Perhaps there was a way to do that with `merge`, but I didn't figure it out. – arr_sea Jan 07 '14 at 21:50
  • "Just prior" in step 3 is important. In step 4, Tortoise kept complaining that the file already exists, even though I had deleted it using a Windows delete. I had to Update my working copy first. (My colleague had deleted several files using the Tortoise delete, and then committed the changes.) TortoiseSVN 1.8.1. – leqid Feb 05 '14 at 17:27
  • A little circuitous, but it works! After selecting "Copy to working copy ..." I had to browse all the way through my project working folders to get to the right location. – yoyo Apr 07 '14 at 22:29
  • I tried using accepted answer but did not work. And tried this it worked perfectly. In my opinion this must be accepted answer. Finally I get back my deleted folders and files from SVN otherwise I may be fired from my office. – Yubaraj Sep 01 '14 at 12:40
  • With newer graphical TortoiseSVN versions, you can simply select the commit where the files were deleted, and then in the files listing below the log, right-click the deleted file and select "Revert to parent revision". At least that works for me. – MC Emperor Jun 30 '16 at 07:06
  • If you've deleted a tag, you can just do a copy-to back to your tag. – iheanyi Oct 09 '17 at 15:47
  • Tortoise SVN 1.14 on Windows: "Copy to working copy" is only from right-click on repository browser, not on log. Use log to get correct version number, use repository browser to find the file in that version, then use "copy to working copy" – david May 16 '23 at 11:27
60

Use svn merge:

svn merge -c -[rev num that deleted the file] http://<path to repository>

So an example:

svn merge -c -12345 https://svn.mysite.com/svn/repo/project/trunk
             ^ The negative is important

For TortoiseSVN (I think...)

  • Right click in Explorer, go to TortoiseSVN -> Merge...
  • Make sure "Merge a range of revisions" is selected, click Next
  • In the "Revision range to merge" textbox, specify the revision that removed the file
  • Check the "Reverse merge" checkbox, click Next
  • Click Merge

That is completely untested, however.


Edited by OP: This works on my version of TortoiseSVN (the old kind without the next button)

  • Go to the folder that stuff was delated from
  • Right click in Explorer, go to TortoiseSVN -> Merge...
  • in the From section enter the revision that did the delete
  • in the To section enter the revision before the delete.
  • Click "merge"
  • commit

The trick is to merge backwards. Kudos to sean.bright for pointing me in the right direction!


Edit: We are using different versions. The method I described worked perfectly with my version of TortoiseSVN.

Also of note is that if there were multiple changes in the commit you are reverse merging, you'll want to revert those other changes once the merge is done before you commit. If you don't, those extra changes will also be reversed.

BCS
  • 75,627
  • 68
  • 187
  • 294
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • I'm on windows and don't have a CLI version of SVN. do you known how to have ortoiseSVN do that? – BCS Jan 29 '09 at 05:04
  • Tortoise has "merge" on the right-click menu. It has boxes to fill in for the rev. Also "Dry Run" to see if you've got it set right. And remember, the result doesn't count until you commit it. You can revert if it all goes haywire. – gbarry Jan 29 '09 at 05:08
  • I'd say the OP has an older version of Tortoise. The newer one has a different (inferior) merge dialog – 1800 INFORMATION Jan 29 '09 at 05:56
  • I have yet to see ANY merge system that didn't suck. Text, files, dirs, Word docs, yuck. I think it's not a solvable problem. OTOH things can suck more or less. :) – BCS Jan 29 '09 at 06:08
  • 1
    If you revert several files and then revert one of the reverts is that unreverting. Ouch! I thing I just hurt something. – BCS Jan 29 '09 at 06:13
  • For Tortoise SVN, it's easier to revert just the one file you deleted by reverse-merging from the change log. (See steps in my post) – davogones Jan 30 '09 at 01:09
  • this seems like a fair answer but not sure why SVN merge is justified over svn copy? – Daniel Honig Sep 09 '09 at 21:21
  • The recommended way is here: http://svnbook.red-bean.com/en/1.7/svn.ref.svn.c.copy.html and here: http://svnbook.red-bean.com/en/1.1/ch04s04.html#svn-ch-4-sect-4.3 – Prince Apr 08 '14 at 19:13
27

For completeness, this is what you would have found in the svn book, had you known what to look for. It's what you've discovered already:

Undoing Changes

Resurrecting Deleted Items

Same thing, from the more recent (and detailed) version of the book:

Undoing Changes

Resurrecting Deleted Items

gbarry
  • 10,352
  • 6
  • 33
  • 43
18

Use the Tortoise SVN copy functionality to revert commited changes:

  1. Right click the parent folder that contains the deleted files/folder
  2. Select the "show log"
  3. Select and right click on the version before which the changes/deleted was done
  4. Select the "browse repository"
  5. Select the file/folder which needs to be restored and right click
  6. Select "copy to" which will copy the files/folders to the head revision

Hope that helps

BCS
  • 75,627
  • 68
  • 187
  • 294
Mukul Joshi
  • 181
  • 1
  • 2
13

I always seem to use svn copy as a server operation so not sure if it works with two working paths.

Here is an example of restoring a deleted file into a local working copy of the project:

svn copy https://repos/project/modules/module.js@3502 modules/module.js

While being inside the project directory. This works as well for restoring entire directories.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • 2
    +1 The copy @ worked. The copy -r does not seem to work for using CollabNet svn 1.6.17 under Mac OSX. – Gray Oct 11 '13 at 18:35
  • 2
    Also the revision used should be the revision right _before_ the deletion. – Gray Sep 09 '15 at 16:25
6

If you're using Tortoise SVN, you should be able to revert changes from just that revision into your working copy (effectively performing a reverse-merge), then do another commit to re-add the file. Steps to follow are:

  1. Browse to folder in working copy where you deleted the file.
  2. Go to repo-browser.
  3. Browse to revision where you deleted the file.
  4. In the change list, find the file you deleted.
  5. Right click on the file and go to "Revert changes from this revision".
  6. This will restore the file to your working copy, keeping history.
  7. Commit the file to add it back into your repository.
davogones
  • 7,321
  • 31
  • 36
5

The easiest way I have been able to restore files and not lose revision history is using SVN copy, the merge example above to me seems like a more complex way to achieve the same thing. Why is there a need to merge when you simply want to restore a revision?

I use the following in this instance and it works quite well.

svn copy -m 'restoring file' -r <rev_number_file_to_restore> http://from/file.cs http://pathTo/file.cs

I always seem to use svn copy as a server operation so not sure if it works with two working paths.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Daniel Honig
  • 4,268
  • 6
  • 26
  • 24
  • 3
    SVN COPY is the recommended approach. Syntax is very simple: svn copy [SVN URL]@[REVISION # WHERE FILE EXISTS] [LOCAL PATH TO RESTORE TO] from http://www.visualsvn.com/support/svnbook/branchmerge/basicmerging/ . Just used this myself. – amit Feb 09 '12 at 07:55
  • This makes most sense to me, I also use this approach – Dmitry Pashkevich Sep 17 '12 at 10:47
4

With Tortoise SVN:

If you haven't committed your changes yet, you can do a revert on the parent folder where you deleted the file or directory.

If you have already committed the deleted file, then you can use the repository browser, change to the revision where the file still existed and then use the command Copy to... from the context menu. Enter the path to your working copy as the target and the deleted file will be copied from the repository to your working copy.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You should be able to just check out the one file you want to restore. Try something like svn co svn://your_repos/path/to/file/you/want/to/restore@rev where rev is the last revision at which the file existed.

I had to do exactly this a little while ago and if I remember correctly, using the -r option to svn didn't work; I had to use the :rev syntax. (Although I might have remembered it backwards...)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • That's not what I want. I want to patch the file back into the repo and have SVN known that it's the same file. – BCS Jan 29 '09 at 05:17