141

If you delete a directory from an SVN working copy, but haven't committed yet, it's not obvious how to get it back. Google even suggests "svn undo delete before commit" as a common query when you type "svn undo d", but the search results are unhelpful.

edit: I'd like a solution that works in subversion 1.4.4

LaC
  • 12,624
  • 5
  • 39
  • 38
  • Are there any sibling directories to the one you deleted with pending changes? If so, be careful when reverting from a higher level you may lose changes. – Russell Nov 23 '09 at 23:13

7 Answers7

160

1) do

svn revert . --recursive

2) parse output for errors like

"Failed to revert 'dir1/dir2' -- try updating instead."

3) call svn up for each of error directories:

svn up dir1/dir2
Denis Barmenkov
  • 2,276
  • 1
  • 15
  • 20
  • 12
    This is the most accurate answer, thank you very much, you helped me a lot. – greenoldman Feb 08 '11 at 06:42
  • 1
    Except that reverts everything? You may not want to do that. – n13 Aug 01 '13 at 03:23
  • 1
    This makes the most sense if you have an entire folder that SVN thinks was deleted. I had that while checking out a working copy. One folder already existed, so it was placed in conflict and was not checked out. It was a link to another folder. I renamed it and tried `svn up` to get it to check out, realized the conflict, did `svn resolved foo` and `svn status` told me I had about 10k files marked as `D`. `svn up` and `svn revert` did not get them back, but this solved the problem. – simbabque Feb 18 '14 at 09:30
  • 6
    +1 for `--recursive` which helps to restore contents of deleted directories. – Melebius Jul 10 '14 at 13:00
  • 1
    Oh my God, you saved my evening! – ArturOlszak Mar 06 '16 at 21:20
  • 1
    Like simbabque, I had a folder in conflict. I tried to `rm` the folder and `svn update` but svn had marked the folder for removal because of the conflict. This solution partially worked for me. It brought back the folders but not the files. Running `svn update` did not bring back the files either. I ended up removing the folder one level up, then using `svn update` brought the files back (thanks @LaC). – Steven C. Howell May 19 '16 at 14:39
147

svn revert deletedDirectory

Here's the documentation for the svn revert command.


EDIT

If deletedDirectory was deleted using rmdir and not svn rm, you'll need to do

svn update deletedDirectory

instead.

Community
  • 1
  • 1
Michael Hackner
  • 8,605
  • 2
  • 27
  • 29
  • 3
    That must be new, it doesn't work in SVN 1.4.4. (It was the first thing I tried.) – LaC Nov 24 '09 at 16:02
  • 4
    This is what 1.4.4 says, btw: $ svn help revert revert: Restore pristine working copy file (undo most local edits). usage: revert PATH... Note: this subcommand does not require network access, and resolves any conflicted states. However, it does not restore removed directories. – LaC Nov 24 '09 at 16:03
  • 1
    1.6.6 has the same text in the help, but it worked for me when I tested it. Note that if you did not use `svn delete` to delete the directory, but instead deleted it using `rmdir`, you'll need to `svn update deletedDirectory` instead. – Michael Hackner Nov 24 '09 at 16:31
  • I have deleted one file (svn del) from a local svn-ed directory. svn update did not work but svn revert worked. I was using SynchroSVN for Mac. However, thanks for the tips. – karim Oct 13 '10 at 08:57
  • I'm still not sure if this works with SVN 1.4.4, but I don't care anymore, so I'm accepting this answer. – LaC Feb 12 '11 at 15:57
  • I mistakenly deleted the entire content of a directory (including the .svn folder), but not the directory itself. The solution was to delete the directory also, and then run `svn update directory_name`. – TRiG Apr 10 '13 at 10:31
  • 5
    As per the link, use svn -R revert to undelete the whole directory with all contents, which is probably what you want. svn revert without the -R just restores the directory, not the contents. – n13 Aug 01 '13 at 03:22
  • This did not work for me. The following commands worked for me: svn revert -R . svn up – Mohd Farid Mar 31 '16 at 16:12
  • This doesn't work. Unacceptable that people here post wrong answers. @MichaelHackner should feel shame. – ABCD Feb 04 '18 at 11:54
  • svn revert --depth=infinity . – iosMentalist Nov 17 '21 at 12:06
33

What worked for me is

svn revert --depth infinity deletedDir
Jason
  • 11,709
  • 9
  • 66
  • 82
  • This worked great for me (svn 1.7.10) where **svn revert deletedDir** failed to do the job. Excellent answer. – rtome May 29 '15 at 12:58
5

Do a (recursive) Revert operation from a level above the directory you deleted.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    This will obliterate WAY more changes than necessary. – Michael Hackner Nov 24 '09 at 14:06
  • That does not work with Subversion 1.4.4 (I've just tried it). svn help revert even says "this subcommand does not require network access, and resolves any conflicted states. However, it does not restore removed directories." I'm sorry, I should have specified the version. 1.4.4 is what comes with Mac OS X 10.5. – LaC Nov 24 '09 at 16:09
  • That worked for me. (I just had one deleted file I wanted to restore.) SVN Tortoise – Jiminion Mar 10 '22 at 16:52
4

To make it into a one liner you can try something like:

svn status | cut -d ' ' -f 8 | xargs svn revert
Earle
  • 89
  • 4
1

The simplest solution I could find was to delete the parent directory from the working copy (with rm -rf, not svn delete), and then run svn update in the grandparent. Eg, if you deleted a/b/c, rm -rf a/b, cd a, svn up. That brings everything back. Of course, this is only a good solution if you have no other uncommitted changes in the parent directory that you want to keep.

Hopefully this page will be at the top of the results next time I google this question. It would be even better if someone suggested a cleaner method, of course.

LaC
  • 12,624
  • 5
  • 39
  • 38
0

You could remove the folder and update the parent directory before committing:

rm -r some_dir

svn update some_dir_parent

xinthose
  • 3,213
  • 3
  • 40
  • 59