How to work in a branch under Subversion source control

And how to merge back to the trunk safely

A common cause for headaches when dealing with version control is the process of merging a branch of code back into the trunk. As a brief primer, software developers will use version control systems to manage changes in their code and allow for multiple developers to work on the same project at the same time. One developer will check out a copy of the code, make changes, and commit them back into the code repository. Meanwhile, another developer can commit the same file back to the repository and the version control system will merge them together (hopefully). There are several popular source control systems (see Git and Mercurial) but in this case I'll be talking about Subversion (or SVN as it's abbreviated).


The main version of the software is commonly called the trunk. This is the codebase used in the production version of the software. It's common for developers to work directly in the trunk when they are making minor changes to the code. Whether they should or should not do that is up for debate. When a new feature comes down the pipeline that will take a significant amount of time to program, it usually makes sense to branch off from the trunk so that developers can work on this feature without interrupting any important changes that need to be made to the trunk code.


When a developer branches off from the trunk, they are creating a copy of the trunk at the point in time that they chose to branch. From there, they can work on and test the new feature in this separate copy of the code. It's important however that the branch version does not get too far out of sync from the trunk version. If you're working on a large feature that might take a month to develop, you don't want to wait until you're done programming to start bringing yourself up to date with the trunk. If you do, you'll increase the number of, and complexity of, code conflicts. Instead, you periodically want to merge the latest version of the trunk back into your branch.


Merging the changes that have been made in the trunk (or any other branch) into your development branch is known as merging a range of revisions. The trunk is not affected by this operation, it's simply bringing into your branch everything that is new since you branched off. You can do this as frequently as you like while you're working on a branch, but it's essential that you do it once just prior to merging the branch back into the trunk.

The steps involved with completing this operation depend on the tools you are using to work with SVN. I recommend TortoiseSVN for an accessible GUI to SVN under Windows. It integrates directly into the Windows Explorer shell and makes working with SVN a breeze. The steps to merge the trunk into your branch can be found in this handy guide. After you complete the merge, don't forget to commit the merged changes back to your branch.

When the work has been completed and tested in the branch, you're ready to reintegrate your branch back into the trunk. Reintegration is the process of bringing the changes that have been made independently in the branch back into the trunk in preparation for release. To do this, you want to switch back to the trunk version of the code and use TortoiseSVN to choose the merge option again. This time however, you want to chose the “Reintegrate a branch” option. In what is basically the reverse of what you did with merging a range of revisions, you choose the branch that you'd like to bring into the trunk and then merge. A detailed guide of this process can be found in the article Branching and Merging with TortoiseSVN.


Once you've resolved any conflicts and you have the branch changes back into the trunk, you can begin testing the changes again on the production line of code. When you're satisfied that everything is as it should be, commit the trunk back to Subversion to prepare for a production deploy of your new feature.


The branch and the trunk have now been reintegrated and you're ready to do it all over again with the next feature. For an in depth guide on how to use the merging features in TortoiseSVN view the Merging guide. For the command line usage of Subversion, check out the command cheat sheet, or the manual for subversion.

ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon