Doh! I needed that file!

Every once and a while, I remove a file (or directory of files) from my local repository that I want back.  No big deal, it's in the repository.  Well, yeah, but finding it and using the right command is not so straightforward.  And since I recently deleted a whole bunch of files and I'm waiting to get them back, I'll show you how.

To recover deleted files from svn, first find the revision in which the file(s) existed:

cd /path/to/local/repository/
svn log --verbose .

I had a lot of files to recover, so I piped the output to a temporary file and cut out the junk I didn't need.

Second step, recover those files:

#!/bin/sh

REPO=https://svn.example.com/svn/repos/example/foo/
REV=11873 # one less than revision when file was deleted
FILES=- # pipe into this script your file names (like from xargs)

cat "$FILES" | while read file
do
    d=$(dirname "$file")
    if [ ! -d "$d" ]; then
        mkdir -p "$d"
        svn add "$d"
    fi
    svn copy -r $REV "$REPO/$file@$REV" "$d"
done

This is not so easy for three reasons.  First of all, if you have directories, you need to get their paths right and create new local directories for them.  Second, if your file names have spaces in them, you need to quote those appropriately.  Finally, you need to use a revision one less than the revision the files were deleted.  In my example, I deleted the files in revision 11874.  So I needed to copy the files as they existed in revision 11873.

Hooray, you now can recover deleted files from svn.  Thanks to m0j0 for getting me started!

 

Trackback URL for this post:

http://www.ideacode.com/trackback/46

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Quick Links