Restoring a deleted file or directory from Subversion

Restoring a deleted file or directory from Subversion (SVN) is possible as all items will continue to exist in previous revisions.

Before you start you will need to know the path of the deleted item and the revision when it was removed.

With the above information, follow the steps below to undelete a file or whole directory from a Subversion repository:

Check out the latest (HEAD) revision for the parent directory of the deleted item into a temp directory, so If the item was at the URL https://svn.example.com/trunk/aproject/file.txt, checkout: https://svn.example.com/trunk/aproject

cd /tmp
svn checkout https://svn.example.com/trunk/aproject

Now the deletion can be checked with a dry run merge using the revision the item was deleted and the reversion before that. Notice in the command below how the revision the item was deleted (4163) appears first in the revision list. The second revision is always one less than the first. With a dry run the merge output will show the changes it would of made.

svn merge --dry-run -r 4163:4162 https://svn.example.com/trunk/aproject .

Once you are happy with what changes the merge will perform, run the command again but without the --dry-run switch.

svn merge -r 4163:4162 https://svn.example.com/trunk/aproject .

If there were other changes made in the revision in which the item was deleted, then these can be reverted in your working (local) copy before commiting the changes from the last merge.

Then commit the changes.

Last updated: 02/04/2012