Saturday, March 3, 2012

SVN: Merging Trunk into Branch

In my case, the repository in question is on Jira.  So instead of using svn+ssh://, I use https:// to access the repository.  I am assuming you already have a branch in your local directory, and you want to merge trunk into it.  This is a good practice.  Otherwise, if you have CSS files with revisions for instance, it's quite possible that both you and another developer might mod those files and advance the revision.  This creates a "local add, incoming add upon merge" tree conflict that subversion doesn't know how to resolve forcing you to resolve it manually.  This sort of problem cannot be completely avoided, but at least you can pick up any changes that have already been put back before you alter the files.  Over the course of a long project, you can end up very far behind trunk with a big job to merge if you don't do this occasionally.

Before you start this process, remember to do a commit of your branch picking up any changes that have made.  This keeps the log files clean when you commit the trunk merge, and it allows you to completely roll back the changes in case something goes horribly wrong.  Assuming you have already committed your branch, proceed as follows:

Check out a copy of trunk if you don't already have one.  Otherwise, just cd to the root of it and proceed to step 2.

          1.  svn co https://server/path/to/project/trunk

cd to the root directory of trunk

          2.  svn up

this will already be up to date if you just checked it out in step one, but note down the revision number (YYYY) it spits out.

cd to your branch root directory

          3.  svn log --stop-on-copy

This will display all the revision that have occurred since this branch was cut from the main one.  Find the lowest numbered revision (the oldest one), and make a note of it (XXXX).

Now execute the merge with the following:

           4.  svn merge --dry-run -r XXXX:YYYY https://server/path/to/project/trunk

Note the "--dry-run".  That's completely optional of course, but if you think there may be conflicts, it's not a bad idea to get an idea of what they may be before you start.  

once you are happy with that, run it for real:

           4.  svn merge -r XXXX:YYYY https://server/path/to/project/trunk

resolve all the conflicts.

           5. svn commit -m "Merged trunk into branch"

No comments:

Post a Comment