Reverting single file in SVN to a particular revision
Asked Answered
C

13

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
Chemaram answered 11/5, 2010 at 17:20 Comment(1)
when you said revert, what did you mean, 7 years ago, lolMid
R
191

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

Rebuttal answered 11/5, 2010 at 17:25 Comment(7)
but the next time you update it gets the file you didn't want back... :SPalecek
if you don't want it back, svn commit that file, to put the version you want into the repoNonsense
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.Cantara
How can I commit the reverted file then?Kassa
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.pyIndistinct
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.Chifley
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.Privity
S
102

svn cat takes a revision arg too!

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

Stylolite answered 24/2, 2015 at 15:35 Comment(4)
This is by far the best answer on the page. It leaves the changed file in a state that can be committed.Maramarabel
this doesn't seem to work when you are pasting into a new fileCelesta
haha looking back at it, idk what context i was thinking of when i posted. I can't recall. It does seem nonsensical.Celesta
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.Adventurous
S
58
svn revert filename 

this should revert a single file.

Siva answered 16/3, 2014 at 1:22 Comment(7)
What's up with all the other answers with longer commands? Thanks.Cambell
You are welcome. I have no idea what is up with other answers.. lolSiva
they're up, and they're longer. i concur.Poisonous
explanation for down voting?Siva
This reverts to which revision? Doc says just replaces with latest in repo. Question is how to revert to an earlier version.Nicki
This reverts to the latest version of repo. If you read the description of the questions, it seems like he was looking for how to revert a single file, not entire directory.Siva
I believe all this does is destroy your working copy by 'reverting' it back to head. Or to which ever was checked out. confirm by doing svn update -r VER file, then do an svn revert.Scrogan
I
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.

Internuncial answered 11/5, 2010 at 17:26 Comment(4)
export, then paste it to your local copy, then commit it again?Nicki
@Nicki that is one way to do it, yesInternuncial
@Nicki and what is the "preferred" way?Lablab
No idea but that is what worked for me : exported past revision. Opened in notepad, copied, pasted in to working file. Committed that to head with comment on which rev and why. UI tools might have a better way.Nicki
J
27

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?

Jenifferjenilee answered 12/11, 2015 at 2:1 Comment(4)
I wish to remark that at least for my version of Subversion, svn merge does not take multiple files as an argument for some reason. In this case it seems to do just nothing (without an error message).Prettypretty
But my command still works by repeating it for each file, right?Legatee
Yes, it does indeed.Prettypretty
Agreed that this is the cleanest.Tiannatiara
B
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.

Bidle answered 11/5, 2010 at 17:31 Comment(0)
E
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.

Exurb answered 19/2, 2018 at 21:57 Comment(0)
M
9

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
Mid answered 21/2, 2017 at 3:16 Comment(3)
if you have comments please discuss here before making edits?Mid
Doesn't the scenario in the question require a reverse-merge of the most recent change, such that we should apply the changes needed to go from HEAD to PREV?Conditioning
The command should be svn merge -r HEAD:PREV file as @Nick Russo saidPalpitate
Z
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.

Zelma answered 27/8, 2013 at 22:40 Comment(2)
then commit the file so it goes to the repoNicki
This worked for me better than update. Because update does not let you commit the modification made to that file. (svn 1.8.3)Peppy
V
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.
Varrian answered 19/11, 2015 at 11:41 Comment(2)
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?Palpitate
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.Varrian
S
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.

Shumpert answered 20/4, 2017 at 2:19 Comment(2)
and this is the best answer :)Farming
why is this the best answer? it doesn't manage history the "svn way"Speedy
T
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).

Toney answered 13/7, 2018 at 23:55 Comment(0)
P
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

Privity answered 1/8, 2018 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.