在 subversion 中取消删除文件的简单方法是什么?

这些说明有点吓人和令人困惑: http://svnbook.red-bean.com/en/1.0/ch04s04.html#svn-ch-4-sect-4.3。 而且他们似乎也没有提到,如果你在做完“ svn rm”之后还没有签入,那么这样做会简单得多。

因此,我认为这将是一个很好的地方,记录一个简单的答案,为那些谷歌这一点。

[1]对于 svn 新手来说,“ svn rm”可能会立即销毁文件。我记得我在做 svn rm的时候,想着只要把它从源代码控制中移除,当文件本身真的消失的时候,我就抓狂了。因此,一个子问题是,如何正确地从版本控制中删除文件,而不必真正删除本地副本?

89699 次浏览

If you just did

svn rm foo.txt

then you can undo that with simply

svn revert foo.txt

If you already checked in after doing the "svn rm" then you can look at the log (svn log), find the last revision where the file existed, and grab it from that version.

One way to do that is to merge in the old revision that has the file. Assuming the current revision is 123 and the last version with that file is 120, then do this:

svn merge -r123:120

Maybe first do a dry run to make sure it won't do anything you don't want:

svn --dry-run merge -r123:120

For the sub-question, how to remove a file from svn without removing the local copy:

svn rm foo.txt --keep-local

Or, of course, you could just copy to a temp file before svn rm'ing and then copy back:

cp foo.txt foo.txt-tmp
svn rm foo.txt
(svn ci -m "just removed foo.txt from the repository")
cp foo.txt-tmp foo.txt

To remove a file but keep it in the working copy, use

svn rm foo.txt --keep-local

To get a file back after you've committed the remove, you can use either the merge command, or the copy command - both will preserve the file history. Since the merge way is already described elsewhere, here's the copy way:

svn rm foo.txt
svn ci foo.txt -m "removed file"
(now at r5 as an example)
svn cp url/to/removed/foo.txt -r4 foo.txt
svn ci foo.txt -m "got file back"

for whatever reason, the -r arg wasn't working for me (svn version 1.6.9). I tried before and after the src, but kept getting a message that the file was not found in the current revision (duh). Same with --revision. Using the @ style syntax worked though:

svn cp url/to/removed/foo.txt@4 foo.txt

i used the copy menu item from the previous revision using tortoise, worked for me and kept the history.

To revert a deleted file in SVN. Find the revision number just before delete (say 341). Then revert that file alone to that revision number.

svn up -r341 [path_to_deletedFile]

For TortoiseSVN users, this is very easily accomplished via the Repo Browser. Within the Repo Browser switch to the revision before the file was deleted (you can change the version using the "Revision:" button at the top right):

enter image description here

You should then be able to see the deleted file in its original location in the Repo Browser. Right-click on it and choose "Copy to working copy...":

enter image description here

The file will then be restored to your working copy ready to commit (as though svn added) and with its log history intact.