It’s generally considered bad practice to work directly in the head revision (aka. the trunk) of an SVN managed project. Undoing changes committed in the head is difficult, and if there are a number of developers working on different features, then updates to the code will be visible to all and may cause problems.

It’s much better to work in a branch and to merge the changes once you’re convinced they are stable. This post demonstrates how to do this using the Subversion command line tools; however, the same principles would apply if you were using a GUI tool like Tortoise for example.

Creating a branch

Creating a branch is simple. You just copy the revision you want to make changes to into the branches folder of your project in the SVN repository, for example:

svn copy http://svnserver/myproject/trunk http://svnserver/myproject/branches/stevescopy

This will create a copy of the head revision in the project called ‘myproject’ in a branch folder called ’stevescopy’ on the SVN server found at ’svnserver’.

You may notice when doing this that the copy operation completes very quickly, this is because no files are actually copied, SVN just copies markers to file versions making branching a very cheap and fast operation to perform.

Next you should check out the branch before working on it in the usual manner. So, following the previous example, we could check out our branch using:

svn co http://svnserver/myproject/branches/stevescopy .

When working on the code in the branch, you should feel free to commit changes as frequently as you like. The changes that you make are now completely separate from the head revision.

Merging changes back to the trunk

When you’re ready to merge whatever updates you’ve been working on back into the trunk, you should first commit all changes in your branch. To do this, issue the SVN commit command from the working directory of your branch:

svn commit -m 'committing the branch before merging'

Next, in a separate directory, check out the version of the project that you want to merge your changes into. In our case we want to merge our changes back into the head revision:

svn co http://svnserver/myproject/trunk .

Now we can perform the merge. The merge does not change the data held in the repository, instead it updates the local copy that we have just checked out. So, from the directory into which we just checked out the trunk, we should issue a command to merge the changes from our branch:

svn merge http://svnserver/myproject/branches/stevescopy

SVN will now update all the files in this working copy with the changes made in our branch. If there are conflicts (i.e. another user has updated the original trunk files since you checked them out) then SVN will detect this and it will ask you to resolve them. Once completed, you should check that your project builds and passes all of its tests before committing your merged copy back to the trunk:

svn commit -m 'committing with updates from the stevescopy branch'

Cleaning up

If you don’t plan to do any more work on the branch, you can delete it using something like this:

svn rm http://svnserver/myproject/branches/stevescopy