SVN Merge Without Conflicts

The Problem

 I had created a branch and wanted to merge changes from the original trunk into that branch, bringing it up to date. Sounds simple enough, just right click on the branch folder and (with tortoise svn) click on Merge. 

 Then keeping all of the wizards default, go through and Merge. Easy!

Except when I do it this way I get a conflict for every file that has changed. Even if it was changed in the trunk but not in the branch, or changed in the branch but not in the trunk. I can’t work with that because then every time I want to merge I’d have to manually resolve conflicts on potentially hundreds of files.


I must be doing something wrong…

Setting Up The Branch

So lets go back a few steps to setting up the branch.

1. Create an empty folder, I called mine MergeTestBranch.
2. Checkout from the trunk.


3. Now create a branch, don’t forget to check the ‘switch working copy to new branch’ box.


Make Some File Changes

Ok so now we have a branch and currently it is an exact copy of the orginal trunk. This is the contents of the directory: filelist1




So now to recreate the merge conflict ‘problem’ what we need to do is:

  1. Change a file in the branch. I just added the word, ‘branch’ to the bottom of fileTwo.txt.
  2. Change a file in the trunk. I just added the word, ‘trunk’ to the bottom of fileThree.txt.
  3. Change a file in both the trunk and the branch. I just added the word, ‘trunk’ to the BOTTOM of fileFive.txt and added the word, ‘branch’ to the TOP of fileFive.txt.
  4. Save the changes.
  5. Commit the changes to the trunk, and commit the changes to the branch.

Before you try to merge…

…make sure your working copy of the branch is “clean”—that it has no local modifications reported (from SVN Book)

In other words, if you are using tortoise SVN, make sure you have the ‘green tick’.

Doing the Merge – Incorrectly

So now if we repeat the merge that I mentioned at the start of this post, i.e. from the trunk into the branches working directory, keeping all of the merge wizard defaults, you get the merge results shown in the first image of this post. 

As you can see, the three files that we changed are all conflicted. Not what you want!

Doing the Merge – Correctly

Of course SVN is doing what it should, the problem is how I am doing the merge. The way to make this work as I want it to (no conflicts for the files we changed) is actually very easy.

1. Before starting the merge, right click on the branch working folder and select Show log.


2. Why are we looking at the log? We need to know the original revison number that this branch was created from. Write down the revision number (in this case 29635)


3. Start the merge in the usual way, keep ‘Merge a range of revisions’ selected and click next.

4. Now the important bit. What we need to do differently is tell SVN the revision range to merge.


 So what we are doing here is telling SVN, I want you to merge changes from the trunk starting from revision number 29635 and going up to the HEAD (doesn’t need to be the head, can be another revision number…of course).

Or in other words, grab all of the changes that have been made in the trunk since I created my branch…and merge those changes into my working copy.




 5. Click Next, test the merge and you should now get this result


Much better!

Of course you will still get conflicts, but only when SVN is unable to automatically resolve it, e.g line 5 of a file was changed in the trunk and the branch.

And finally, automatic resolution of conflicts makes life alot easier when merging but remember that there’s no way SVN can detect symantic differences in your code so occassionally the auto merge will merge correctly in terms of file differences but may cause problems in the code’s logic…though in my experience this is very rare. How do you cater for this problem though? Simple, write unit tests 🙂 and make sure you have a Continuous Integration server setup!

Happy merging!

  1. Nice one!

    Here is another posting:

    • pedro
    • September 9th, 2009

    you saved my ass!!! thanks a lot.

    • alberlau
    • October 20th, 2009

    And what about modifying and merging back from branch to trunk? Will merge process be correct?

    • jol blazey
    • October 22nd, 2009

    The above method wont always work especially in a big project. A more detailed explanation:

  2. Unfortunately merging is a very manual process with SVN. I usually keep a file around that keeps track of the starting branch ( usually trunk ) and revision (or you can use ‘svn log –stop-on-copy’ to find it). Then I keep track of the rest of the merges from trunk into the branch in the commit messages.

    I think the most important part is to review ( svn diff ) your merges before you commit. It is also difficult to merge if there are renamed or deleted files. This usually requires a manual diff and patch apply for each of those files.

    If you always make sure that you are up-to-date with trunk then the final merge back will be clean. All of the sequential merges from trunk to branch will look like “svn merge -r123:126 [url-to-trunk]” performed within the branch’s working copy; but the final merge back will be “svn merge [url-to-trunk] [url-to-branch]” performed in trunk’s working copy. The first form is “apply the diff between revisions trunk-123 and trunk-126 onto the branch working copy”. The second form is “apply the diff of [trunk] and [branch] onto the trunk working copy”.

    I am a huge fan of git and am using it for all of my new projects. I do have a few remaining SVN repos around and find myself using “svn diff” and linux’s “patch -p0 < patch.diff" a lot now. I think that this is because using git gave me a better understanding of how SVN works ( or should work ).

    Happy merging!

  3. I think a lot of people get confused because they don’t understand what is really happening during a conflict. There is a really simple tutorial that explains why conflicts happen in subversion and how to solve them:

  1. January 7th, 2011

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: