SVN: restore deleted files

One good thing about subversion(svn) is that it does not only keep track of changes you make, it also offers the flexibility to roll back to previous versions. Restoring deleted files is one extreme case in this sense.

For example I want to recover the file filen.txt at r25.

$ svn update -r 24 file.txt
A file.txt
Updated to revision 24.

This will pull file.txt from the repository down to local working directory, but it will disappear after you do another update.

$ svn update
D file.txt
Updated to revision 25.

One way to avoid this is to use full path name in repository.

$ svn copy -r 56 svn://svn.imager.umro/masdar/trunk/mantis/mantis2_Gechuan/file.txt file.txt
A file.txt
$ svn st
A + file.txt
$ svn commit -m 'recover file.txt'
Adding mantis2_Gechuan/file.txt

Committed revision 26.

Of course there are other more clumsy ways to do this. You can pull down the file from repo using relative path, change it, commit, then change it back, commit. This causes unnecessary log entries and can be confusing as time elapses.

The fourth column is `+' when we do svn st. This item is “scheduled with addition-of-history” according to the svn red book, which means it came with its own history that can be rolled back upon. c.f. here.

The fourth column will only show whitespace or a + which means that the file or directory is scheduled to be added or modified with additional attached history. This typically happens when you svn move or svn copy a file or directory. If you see A +, this means the item is scheduled for addition-with-history. It could be a file, or the root of a copied directory. + means the item is part of a subtree scheduled for addition-with-history, i.e. some parent got copied, and it’s just coming along for the ride. M + means the item is part of a subtree scheduled for addition-with-history, and it has local modifications. When you commit, first the parent will be added-with-history (copied), which means this file will automatically exist in the copy. Then the local modifications will be uploaded into the copy.

In case of wanting to recover a file a long time ago, use
svn log --verbose | grep -A5 -B5 file.txt
to show relevant log parts.

related links:

SVN: restore deleted files

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s