SVN branching & merging

This post describes some branch & merge scenarios for subversion repositories.  For more info have a look at the svn book. The variant described here is obsolete as of svn server version 1.5.

Reintegrate a branch to trunk

Check, where the branch started:

 $ pwd
 $  svn log --verbose --stop-on-copy .
 r2664 | bob | 2009-09-08 10:32:03 +0200 (Di, 08. Sep 2009) | 2 Zeilen
 Geänderte Pfade:
    A /branches/branch_xy (von /trunk:2663)

Clean up your working copy of the trunk:

$ pwd
$ svn status
$ svn up
Revision 2695.

So no changes are in your working copy and the head is revision r2695. Now merge:

$ pwd
$ svn merge -r 2665:2695 .
U ...
A ...

Remark the off by one to the revision, where the branch started. Then check, build, test your merged working copy. Afterwards checkin your changes:

$ pwd
$ svn ci

Synchronize a branch with the trunk

If this is the first sync command use the branch revision like in the chapter before. Otherwise use the revision of the last sync.

$ pwd
$ svn merge -r last_sync_or_branch_copy_rev:HEAD .

Afterwards you have synched your branch with the trunk and can commit the changes to your branch. This way reintegration of the branch to the trunk will be easier.

Reintegrate a fully synchronized branch to the trunk

Do it the same way like merging an unsynchronized branch. Deletions and additions will be marked as tree conflicts like this:

!     C path/to/file1
      >   local delete, incoming delete upon merge
      C path/to/file2
      >   local obstruction, incoming add upon merge

Since you have synchronized the branch, all adds/deletes and moves are present in the branch and the trunk. So if you successfully synchronized the trunk with the brunch before you should be safe to mark these tree conflicts as resolved:

$ svn resolve --accept working path/to/file1 path/to/file2

Before you check in the merged, you might use a little script to compare the trunk and the branch compare working copies. Of course you should build and test the changes, too.

Further thoughts

You can always execute a merge command and check the changes with svn diff. If the result does not match your wishes you can revert your changes with svn revert -R ..

One thought to “SVN branching & merging”

Leave a Reply

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