Thursday, May 3, 2007

SVN Merging using SubClipse

Up until a few days ago I was laboriously merging in changes in Subclipse by hand: copying files across manually, trying to make sure I didn't miss anything. Then I discovered the merge tool: Now I can merge in complex changes that would have taken hours in just a few minutes.

The process is remarkably simple: Start by right click the root of the tree that you want the code to be merged into (in the file browser) and go to Team -> Merge. This gives you a somewhat unintuitive dialog: it took me a while to figure out that it was only asking for a source to merge from (even though one section does say To:). For most reasonable cases you want to use the same SVN path in the From: and the To: sections. In the From revision box you will want to put the revision which you last merged in, and in the To section select Head Revision.

Now you're ready to go: just hit Merge and SVN will work its magic (although it might take a while - be patient). Once it has finished it will display a log of its actions in the Console window, most of it will have worked fine - but there will probably be some conflicts, marked with a red C. All you have to do here is locate the relevant files in the file browser, right click on them and hit Team -> Edit Conflicts. This will open a new editor window with two panes, you're version on the left and the other version on the right.

In the Edit Conflicts window all conflicts will be highlighted, and all you have to do is resolve them by making suitable changes to the left-hand pane and saving (this is easy: they're rarely more than a few lines of code). Once things are sorted out, right click on the file in the file browser again and select Team -> Mark as Resolved. Once you've resolved all the conflicts you're done. Subclipse really is a wonderful piece of software.

One last tip: I find it useful to commit my branch just before merging changes: that way if something goes wrong I just have to hit Team -> Revert and everything is better again.

Note: For those of you who haven't tried Eclipse, Subclipse is an Eclipse plugin that adds SVN support - I use it in conjunction with Pydev.

11 comments:

Samuel said...

I don´t know why but when I do this process nothing happens. I know that I have made a lot o changes in my branch but when a try to merge it to the trunk nothing happens.

Anonymous said...

Try to use the From Revison where you created your branch plus one.
And the to Revision the last one you have in the lookup. Than it should take all changes!

Anonymous said...

You need to do the "Merge" operation on the branch that you want to make the changes to. But you set the URL in the Merge dialog box to the URL of your branch where you made your changes in (this is the from part).

Basically, what happens is that a diff is made in your old working branch (for me, its branches/tomslocalchanges/), and the diff is attempted to be applied to the new branch (for me, its trunk/).

I would right click on trunk/ branch, and make the URL the branches/tomslocalchanges/ branch.

Hope this makes sense

Anonymous said...

for Samuel's query - In the From Revison, select Revison check box and click on Show Log and select previous comitted version. In To Revision select Head Revison check box and hit Merge button.

Jaakko Sipari said...

One last tip: I find it useful to commit my branch just before merging changes: that way if something goes wrong I just have to hit Team -> Revert and everything is better again.

It's not just "useful" to commit before merge but pretty important for a relevant change history. If you've been working on a "red_button" branch, you only want the "red_button" stuff in your commit after the merge to trunk.

Anonymous said...

So you do you manage a merge failure of a binary file?

When you select edit conflicts it comes up with only an error dialog with a big red X in and - not even any text associated with it.

I end up with:
binary.file
binary.file.merge-left.rXXX
binary.file.merge-right.rXXX

I expect the merge to fail, as it can not (nor do I expect it to) manage the merging of binary files.

I can work out which one I want, but I've yet to work out how to get subclipse to allow me to manage the process. How/what do I select to resolve the differences?

The only means that I've worked out so far is deleting and renaming as appropriate - which is not pleasant!

-Chris

LeeMeador said...

The problem I had centered around misunderstanding "to" and "from". I thought that referred to the branches I was merging to and from. It doesn't.

You always merge in the context of some workspace on your machine. It will be tied to some branch (or the trunk). That will be the "TO" branch.

When you select to "merge" you have to pick the branch "FROM" which you are getting the changes to merge into your workspace.

"to" and "from" on the merge dialog box refer to a range of revisions in the "FROM" branch. You don't always want to merge everything back to day 1 so you have to pick a start point and end point in the evolution of the branch (or its creation, if you prefer). That's what 'to' and 'from' mean there.

`Prabu said...

How do I merge in case other developers are working in branches/dev1.1.0 and I (alone) am working on branches/sit but I want to merge to and fro?



(i.e., I should send my changes to dev1.1.0 and rececive their changes to mine)

Please help. Thanks in advance.

Anonymous said...

Thanks a lot for your information. It has really helped me out doing the merging.

Mark Phippard said...

This post is old, but it is going around Twitter today.

Note that a shiny new merge wizard was added to Subclipse after this post was created. It is called the CollabNet Merge Client and is available on the Subclipse update site.

Some details here:

http://desktop-eclipse.open.collab.net/servlets/ProjectProcess?pageID=MEuUjb

Seval said...

I wrote step by step howto document to solve this problem http://chocotech.blogspot.com/2012/10/how-to-merge-two-svn-branch.html :) Your post was very helpful, so I benefitted it during merging. Thanks.