201

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
Gökhan Sever
  • 8,004
  • 13
  • 36
  • 38

13 Answers13

190

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?".

Community
  • 1
  • 1
Eric Hauser
  • 5,551
  • 3
  • 26
  • 29
  • 9
    but the next time you update it gets the file you didn't want back... :S – andygoestohollywood Nov 15 '13 at 09:53
  • 2
    if you don't want it back, svn commit that file, to put the version you want into the repo – Packet Tracer Jan 27 '14 at 10:21
  • 16
    This probably does not actually do what the question asker wanted. Using this, the working copy is clean and cannot be committed AS IS, and an "svn update" without a version number specified will retrieve the latest, unwanted version. – CXJ Feb 19 '14 at 02:35
  • 1
    How can I commit the reverted file then? – marcio Feb 29 '16 at 15:34
  • 4
    This isn't quite right because, as CXJ mentions, there isn't a way to commit the updated file. You probably want to do as Mitch Dempsey says (note, however, the --force to force overwriting the file): svn export --force -r 147 myfile.py myfile.py – benbotto Apr 12 '16 at 20:51
  • It is not complete. I've moved the file to file.old, did svn up again, moved file.old to file (overwriting the original) and finally committed. – Valentin H Nov 07 '16 at 11:11
  • If you want to roll back an individual file and be able to commit, then do: 'svn merge -c -[OldRev#] [Filename]' ie. svn merge -c -150 myfile.py. – Olmstov Aug 01 '18 at 13:52
102

svn cat takes a revision arg too!

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

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Never Sleep Again
  • 1,331
  • 1
  • 9
  • 10
  • 31
    **This is by far the best answer on the page.** It leaves the changed file in a state that can be committed. – dotancohen Mar 02 '16 at 14:08
  • this doesn't seem to work when you are pasting into a new file – ahnbizcad Mar 04 '16 at 21:19
  • 1
    haha looking back at it, idk what context i was thinking of when i posted. I can't recall. It does seem nonsensical. – ahnbizcad Apr 12 '16 at 21:34
  • This is simple and straightforward for when you simply want to undo your changes and get a file back into it's pre-commit state without any version control technicalities. – pretzlstyle Jan 30 '18 at 21:16
58
svn revert filename 

this should revert a single file.

thestar
  • 4,959
  • 2
  • 28
  • 22
50

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.

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
26

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: 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 What is the correct way to restore a deleted file from SVN?

Community
  • 1
  • 1
Michel Müller
  • 5,535
  • 3
  • 31
  • 49
13

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.

Xavi
  • 20,111
  • 14
  • 72
  • 63
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
11

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.

Jennifer Elyse
  • 143
  • 1
  • 9
8

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
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
7

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.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
Joseph Coco
  • 404
  • 6
  • 11
5

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.
bjaastad_e
  • 691
  • 6
  • 10
  • This saves a new unversioned file such as file-xxxx wherein xxxx is the old revision number. What we do next? delete the new revision (HEAD e.g.), rename the new file, add, commit? – Heinz Jan 19 '18 at 15:49
  • No, don't save it with the revision number in the file name. Save it with exactly the same file name as the current version of the same file (in other words, no xxxx in the file name). Then just do an ordinary commit after that. – bjaastad_e Feb 06 '18 at 23:56
4

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.

Kristina
  • 93
  • 7
2

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).

1

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

Olmstov
  • 563
  • 7
  • 18