4 Steps to Painless SVN Branching

Doing some work that can’t be done in trunk in small increments? Then it time to branch.

Say your working on bug “12345 Wibble”…

Step 1: Create the branch

Create a branch of trunk, making a note in the commit message of the revision of trunk the branch is being created from:

svn copy -r 20000
svn://svn/trunk
svn://svn/branches/trunk_wibble/
-m "Bug 12345 Wibble -
Creating branch for feature work 
svn copy -r 20000 
svn://svn/trunk 
svn://svn/branches/trunk_wibble/"

There are now two types of commits you’ll be doing to this branch:

1 – Work for the feature
2 – Resyncing with trunk.

Step 2: Doing the feature work

The feature work should be tracked against a bug as normal:

svn commit -m "Bug 12345 Wibble
- Added Foo for Wibble"

Step 3: Merging changes from trunk into your branch

The commit messages for the merges from trunk are crucial for the book keeping of your branch. Above we branched at revision 20000, suppose trunk is now at revision 30000. So to get those changes merged over in your branch, sit in trunk_wibble, with no other local changes:

svn merge -r 20000:30000 svn://svn/trunk .

Once this is compiling commit it:

svn commit -m "Bug 12345 Wibble
- svn merge -r 20000:30000 svn://svn/trunk ."

Now next time you want to sync your branch with changes that have happned in trunk, all you need to do is look down the list of changesets to see what revision you last synced too.

Step 4: Merging back into trunk

Now your feature is done and dusted, its time to merge to trunk. You need to know 2 things here:

1 – What revision of your branch that was last synced to trunk, typically this will be the last commit you did you your branch
2 – What revision of trunk you last synced to, this will be in your commit message for that final commit.

Suppose these are 40000 and 5000.

Sit yourself in trunk at the revision you last synced to:

svn merge 
svn://svn/trunk/@50000 
svn://svn/branches/trunk_wibble/@40000 .

Depending on how many changes you’ve made this may take a while. Be sure to give the changes a check over to check that match what you think you’ve changed.

svn commit -m 
"Bug 12345 Wibble
Merging feature work into trunk
svn merge 
svn://svn/trunk/@50000 
svn://svn/branches/trunk_wibble/@40000 ."

And your done!

A final note about that last bit

I’ve seen a lot of confusion about this last step. What your not trying to do is merge each of your feature work changesets into trunk.

You’re just after a delta between trunk and your branch so you can apply that to trunk to make it the same as your branch, thats it.

So, what happened exactly when we did the merge back into trunk? That merge was really a diff between trunk and your branch, followed by the application of that patch. You could actually achieve a similar result by doing

svn diff
svn://svn/trunk/@50000 
svn://svn/branches/trunk_wibble/@40000 > wibble.patch

And then applying the patch manually.

patch -p0 < wibble.patch

However you’d end up with empty files for those that had been removed in your branch, and files that needed to be svn add’ed for those that had been added in your branch. Doing it the svn merge way does all the deleting and adding for you.

2 Comments

  1. Thanks Findlers, I’ll give that a go next time I’m working in a branch – looks like a great little tool, I’d not heard of it before!

Leave a Reply

Your email address will not be published.