Question

I have svn repo and I've removed a file somewhere in the past, how can I fetch that file from svn history? Let say I have right now rev 2000 and the file was removed in 1950.

Was it helpful?

Solution

The svnbook has a topic about how to handle this.

It basically boils down to:

  • If the file deletion was the only thing in the commit, you can use svn merge -c -revnum to reinstate the file.
  • If there were other changes in the commit, you can copy the file from the version before it was deleted -- svn copy file@revnum file

OTHER TIPS

If you just want to 'get' the file, you can do:

svn cat -r 1950 file > file

but as has been pointed out, this won't retrieve any of the file history, and will be seen by SVN as a completely new file.

  1. First, run svn log -v and see if you can find the revision that removed the file.
  2. Once you locate where the file was deleted, you can use svn merge or svn cp to get the file back.

Let's say you need to restore foo.txt in trunk/proj/mydir:

$ svn log -v $REPO/trunk/proj/mydir | less

Then you can use /D .*foo.txt to search the less output for the file. You find that the file was deleted in revision 1235:

If that was the only thing revision 1235 did, or you decide you want to undo the rest of the stuff in that revision, you can use svn merge -c:

$ svn co --depth=immediates $REPO/trunk/proj/mydir  # No need to checkout sub directories too
$ cd mydir
$ svn merge -c -1235 .                              # Undoes the delete
$ svn commit -m "Restored file 'foo.txt' deleted in revision 1235"

Sometimes you simply want to restore the file, and not all the other changes in that revision. In that case, use the svn cp command. You could do this without doing a checkout if you prefer:

$ svn cp -r1234 -m"Restored $foo.txt removed in rev 1235" \
       $REPO/trunk/proj/mydir/foo.txt@1234 $REPO/trunk/myproj

Or you can do it though a checkout:

$ svn co --depth=immediates $REPO/trunk/proj/mydir  # No need to checkout sub directories too
$ cd mydir
$ svn cp -r1234 foo.txt@1234  .                     # Undoes the delete
$ svn commit -m "Restored file 'foo.txt' deleted in revision 1235"

Note that I am using revision 1234 and not 1235 because the file was deleted in revision 1235 and doesn't exist in that revision. I use -r to specify that I want revision 1234 of foo.txt and I specify @1234 on the end of the URL to specify that the file exists in revision 1234of the repository directory layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top