Reverting single file in SVN to a particular revision

SvnRevert

Svn Problem Overview


I have a file as shown below in an SVN repo that I would like to revert to a previous version. What is the way to do this in SVN? I want only downgrade this particular file to an older version, not the whole repo.

Thanks.

$ svn log myfile.py
----------------------
r179 | xx | 2010-05-10

Change 3
----------------------
r175 | xx | 2010-05-08

Change 2
----------------------
r174 | xx | 2010-05-04

Initial

Svn Solutions


Solution 1 - Svn

If you just want the old file in your working copy:

svn up -r 147 myfile.py

If you want to rollback, see this "How to return to an older version of our code in subversion?".

Solution 2 - Svn

svn cat takes a revision arg too!

svn cat -r 175 mydir/myfile > mydir/myfile

Solution 3 - Svn

svn revert filename 

this should revert a single file.

Solution 4 - Svn

For a single file, you could do:

svn export -r <REV> svn://host/path/to/file/on/repos file.ext

You could do svn revert <file> but that will only restore the last working copy.

Solution 5 - Svn

So far all answers here seem to have significant downsides, are complicated (need to find the repo URI) or they don't do what the question probably asked for: How to get the Repo in a working state again with that older version of the file.

svn merge -r head:[revision-number-to-revert-to] [file-path] is IMO the cleanest and simplest way to do this. Please note that bringing back a deleted file does not seem to work this way[1]. See also the following question: https://stackoverflow.com/questions/345997/better-way-to-revert-to-a-previous-svn-revision-of-a-file

[1] For that you want svn cp -r [rev-number] [repo-URI/file-path]@[rev-number] [repo-URI/file-path] && svn up, see also https://stackoverflow.com/questions/490522/what-is-the-correct-way-to-restore-a-deleted-file-from-svn

Solution 6 - Svn

The best way is to:

svn merge -c -RevisionToUndo ^/trunk

This will undo all files of the revision than simply revert those file you don't like to undo. Don't forget the dash (-) as prefix for the revision.

svn revert File1 File2

Now commit the changes back.

Solution 7 - Svn

You want to do

svn merge -r [revision to revert from]:[revision to revert to] [path/filename]

Once you do that, you will have that revision of the file in a committable state. Commit the file.

Solution 8 - Svn

surprised no one mentioned this

without finding out the revision number you could write this, if you just committed something that you want to revert, this wont work if you changed some other file and the target file is not the last changed file

svn merge -r HEAD:PREV file

Solution 9 - Svn

I found it's simple to do this via the svn cat command so that you don't even have to specify a revision.

svn cat mydir/myfile > mydir/myfile

This probably won't role back the inode (metadata) data such as timestamps.

Solution 10 - Svn

If it's only a couple of files, and if you're using Tortoise SVN, you can use the following approach:

  1. Right click on your source file, and select "TortoiseSVN" -> "Show log".
  2. Right click on a revision in the log, and select "Save revision to...".
  3. Let the old revision overwrite your current file.
  4. Commit the overwritten source file.

Solution 11 - Svn

Just adding on to @Mitch Dempsy answer since I don't have enough rep to comment yet.

svn export -r <REV> svn://host/path/to/file/on/repos --force

Adding the --force will overwrite the local copy with the export and then you can do an svn commit to push it to the repository.

Solution 12 - Svn

An alternate option for a single file is to "replace" the current version of the file with the older revision:

svn rm file.ext
svn cp svn://host/path/to/file/on/repo/file.ext@<REV> file.ext
svn ci

This has the added feature that the unwanted changes do not show up in the log for this file (i.e. svn log file.ext).

Solution 13 - Svn

If you want to roll back an individual file from a specific revision and be able to commit, then do:

svn merge -c -[OldRev#] [Filename]

ie. svn merge -c -150 myfile.py

Note the negative on the revision number

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionG&#246;khan SeverView Question on Stackoverflow
Solution 1 - SvnEric HauserView Answer on Stackoverflow
Solution 2 - SvnNever Sleep AgainView Answer on Stackoverflow
Solution 3 - SvnthestarView Answer on Stackoverflow
Solution 4 - SvnMitch DempseyView Answer on Stackoverflow
Solution 5 - SvnMichel MüllerView Answer on Stackoverflow
Solution 6 - SvnkhmarbaiseView Answer on Stackoverflow
Solution 7 - SvnJennifer ElyseView Answer on Stackoverflow
Solution 8 - SvnKalpesh SoniView Answer on Stackoverflow
Solution 9 - SvnJoseph CocoView Answer on Stackoverflow
Solution 10 - Svnbjaastad_eView Answer on Stackoverflow
Solution 11 - SvnKristinaView Answer on Stackoverflow
Solution 12 - SvnRobert LelandView Answer on Stackoverflow
Solution 13 - SvnOlmstovView Answer on Stackoverflow