SVN – maintaining code history through reorganization

The following is some advice I’ve given to my student programmers from time to time:

Occasionally, you may need to split up a file in one of your projects into more than one file. Or, you may need to combine two pieces of similar code into one reuseable piece of code. This kind of code refactoring happens all the time, but unless you are careful you may be losing valuable revision history behind those lines of code.

For example, if you split a file into two files you may cut the code from an existing file, and then paste it into a new file. You would then add the new file to your working copy and commit it. In doing so, you would have lost the revision history of those cut & pasted lines.

To get around this issue, instead use SVN to make a copy of the versioned file you wish to split up. If you are using Tortoise SVN, this would be a right-click drag and a “SVN copy and rename versioned files here…” Then, you can remove the appropriate lines from both files and commit. The revision history of all of your lines will still remain!

Unfortunately, the act of merging near-duplicate code into a single file is not as graceful. Instead, you must pick the file closest to the generic end-result (or the file with the biggest history) and modify it to be generic. To avoid losing some of the history, you should note in the commit log what files you are consolidating.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.