Blog: PHP, Python, Linux, Web services & Continuous delivery

Short guide to branching and merging in subversion from the command line

Although my preference these days is to use GIT for SCM, due to legacy reasons I’m still using Subversion for many projects and this post aims, for the sake of my own memory, to provide a short example of traditional subversion branching that will cover:

I prefer to do this just using svn rather than using the svnmerge tool(s), those tools do work OK but I prefer to retain total control. Wherever possible always use the --dry-run option to test the command out before running them for reall as this gives you a (sometimes lifesaving) chance to change your mind.

Creating a branch

svn cp https://svn.repo/trunk https://svn.repo/branches/branch-name -m "create new branch branch-name"

Cuts the new branch 'branch-name' from the trunk. A branch is just a copy in subversion and all copies are shallow copies aka 'copy on write' so no need to worry about bloating the central SVN repository.

Merging changes in the trunk into a branch

So time passes and you make several commits in your branch but there has also been a few changes in the trunk that you need integrate back into your branch. Its important to merge the trunk into the branch regularly to avoid too much drift, do this religiously and it will save you from the pain of a conflict pile up in the long run.

1) Get the initial revision number of the branch

cd /branch-name/
svn log --stop-on-copy | grep ^r | tail -1
# outputs r3245 | ade | 2012-09-05 16:51:18 +0100 (Sun, 05 May 2012) | 1 line

Copy the revision number printed, in the above case it is 3245, ready for the next step.

2) Merge the changes into your branch

cd /branch-name/
svn merge --dry-run -r3245:HEAD https://svn.repo/trunk .

This command will simulate the merge and show what files have been updated or added. If there will be any conflicts they will be flagged with the status code 'C'. When your ready to do the merge for real just remove the --dry-run flag and re-execute the command.

3) Commit the merge

After step 2 has completed successfully just run standard commit to the changes are persisted to the branch

svn ci -m "Updated branch branch-name from trunk"

Merge branch Back into trunk

To help avoid conflicts its important to use the --reintegrate switch which tells subversion that we are merging a child branch back with its parent.

cd /copy/of/trunk/
svn merge --dry-run --reintegrate svn+ssh://server/path/to/branch/myBranch

Like before when your ready to do the merge for real just remove the --dry-run flag and re-execute the command.

Tagging trunk as version X.X.X

Tag trunk, usually using a semantic version number (X.X.X)

svn cp https://svn.repo/trunk https://svn.repo/branches/tags/X.X.X -m "Tag trunk for version X.X.X"
comments powered by Disqus