SVN undo delete before commit
Asked Answered
T

7

142

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

Teirtza answered 23/11, 2009 at 23:5 Comment(1)
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.Chicago
S
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.

Scamander answered 24/11, 2009 at 14:4 Comment(10)
That must be new, it doesn't work in SVN 1.4.4. (It was the first thing I tried.)Teirtza
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.Teirtza
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.Scamander
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.Skipbomb
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.Teirtza
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.Woolard
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.Gannes
This did not work for me. The following commands worked for me: svn revert -R . svn upRounder
This doesn't work. Unacceptable that people here post wrong answers. @MichaelHackner should feel shame.Supposing
svn revert --depth=infinity .Gravamen
U
161

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
Understandable answered 15/9, 2010 at 12:43 Comment(6)
This is the most accurate answer, thank you very much, you helped me a lot.Deaden
Except that reverts everything? You may not want to do that.Gannes
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.Transpierce
+1 for --recursive which helps to restore contents of deleted directories.Mellophone
Oh my God, you saved my evening!Excess
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).Giacomo
S
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.

Scamander answered 24/11, 2009 at 14:4 Comment(10)
That must be new, it doesn't work in SVN 1.4.4. (It was the first thing I tried.)Teirtza
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.Teirtza
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.Scamander
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.Skipbomb
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.Teirtza
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.Woolard
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.Gannes
This did not work for me. The following commands worked for me: svn revert -R . svn upRounder
This doesn't work. Unacceptable that people here post wrong answers. @MichaelHackner should feel shame.Supposing
svn revert --depth=infinity .Gravamen
R
33

What worked for me is

svn revert --depth infinity deletedDir
Roddie answered 12/3, 2014 at 18:54 Comment(1)
This worked great for me (svn 1.7.10) where svn revert deletedDir failed to do the job. Excellent answer.Dexter
T
5

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

Til answered 23/11, 2009 at 23:8 Comment(3)
This will obliterate WAY more changes than necessary.Scamander
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.Teirtza
That worked for me. (I just had one deleted file I wanted to restore.) SVN TortoiseCelery
P
4

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

svn status | cut -d ' ' -f 8 | xargs svn revert
Pileup answered 25/7, 2013 at 15:56 Comment(1)
I can create directory with spaces in its name. It may break 'cut' AFAIK.Understandable
T
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.

Teirtza answered 23/11, 2009 at 23:8 Comment(1)
That is assuming there are no subdirectories in the parent that have pending changes.Chicago
R
0

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

rm -r some_dir

svn update some_dir_parent

Righthanded answered 12/7, 2017 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.