Using Subversion, how can I cut from one file and paste to another preserving history

svn, tortoisesvn

Solution

You can merge all revisions (or specific revisions) of one file into another like this

svn merge sourcefile targetfile -r 0:HEAD

At first I thought one would have to use the `--ignore-ancestry` option (since both files don't share any common history) but apparently this is not necessary. I tested with svn 1.6.3.

You are of course very likely to get alot of conflicts markers in the merge result. It may be easier to do the merge by hand (a copy and paste merge as you say), and then run the above merge command with `--record-only` to tell subversion about it.

After the merge, the `targetfile` will have a `svn:mergeinfo` property which indicates which commits from `sourcefile` where merged into it. When you examine the log of `targetfile`, you can see the history of both files by using the `--use-merge-history` option. TortoiseSVN has the same feature in the form of a checkbox in the log form.

Problem

The situation is that I've spent some time messing around with some experimental code. I now want to move part of that code - about 500 lines - into another file, but I don't want to lose the history, as I would if I do a simple text-editor cut and paste. As close as I know how to get is separating the code out of the original file - svn copy, then delete unwanted stuff from both copies. But I don't know how to then append that partial copy onto an existing file, keeping the history from both. The reason this is important is basically that the code is just pretty specialised stuff, to help implement some higher level functions. I don't want it polluting global namespaces, so I want it all in the one file where it will be used and wrapped in an anonymous namespace. I realise this sounds like merging a branch back into the trunk. The thing is, there is no branch. The experimental code didn't start as a copy of anything - it's just a bunch of started-from-scratch code. The file I want to cut from and the one I want to paste into are completely independent files. I mostly use TortoiseSVN, but have command-line subversion installed too.

Original source