Tools to Compare and Merge Code Better in TFS Source Control

VS WinMerge Compare

Do you use source control? Whether it’s TFS or another of the many good source control options available, there are likely times you have needed to compare or merge code within your source control system. Sometimes I’m reviewing something my team is getting ready to release to production. Other times I have a local copy of something I’ve been working on that may not be fresh in my mind, and I check it against source control to remind myself what changes I’ve made. Whatever the reason, having a good tool to do comparisons and merges of your files can mean the difference between a frustrating experience and a simple task.

Alternative Compare and Merge options

The built in compare tool in Visual Studio 2008 is a bit plain and option-limited:

VS Default 2008 Compare

I prefer the open source WinMerge application¬†(pictured at the top of this article) to the compare tool that’s built into Visual Studio. There’s also a PortableApps version of WinMerge available, for you fellow portable application enthusiasts. WinMerge has lots of window and coloring options that make identifying and merging file differences easier.

Diffuse is another good open source alternative for comparing and merging text that I find myself using more often. It’s got a nice, clean look with colors that are easy on the eyes and is similar in layout to WinMerge, but doesn’t have as many customization options. What it does have is the ability to perform merges on 3 or more file sources at once. This can be useful, for example, if you are looking at merging your changes and the changes included in a branch against original trunk code in your source control system.

VS Diffuse compare

Visual Studio 2012 has improved quite a bit on how it visually distinguishes file differences.¬† It looks significantly nicer than it did in 2008, but it still doesn’t boast the features and customizability that these external tools offer.

VS Default 2012 Compare 2

Setting Up Visual Studio to Use an Alternate Compare / Merge Tool

Many source control systems provide options for using external programs for handling compare, diff, and merge operations. As it turns out, it’s simple to set up Visual Studio to do this too if you use TFS for your source control system. Open up the Tools > Options menu in visual studio. There, expand the Source Control section, select the Visual Studio Team Foundation Server item, and click the Configure User Tools button.

VS TFS Configure User Tools - Compare

Click the Add button in the Configure User Tools window and fill in the available options similar to the following:

VS TFS Configure User Tools - Compare

There, that’s it, now whenever you invoke the compare option on a file in Visual Studio it will use the external tool you just set up.¬† And keep in mind, you can follow the same process to set up one of these tools for the merge operation as well.

Why Use an Alternative Compare or Merge Tool?

Alternative compare and merge tools like WinMerge and Diffuse are specifically made for performing these types of operations and are usually more capable at doing so. I find these two tools often are able to figure out how to align the files you are comparing more intelligently to better visualize the similarities and differences. The options available in these external tools are also quite useful. Maybe you don’t care about blank lines, whitespace, tabs vs spaces, line endings, and those types of differences, and only want to see where actual code has changed. Easily done with the selection of a few options in these tools.

When you’re using Visual Studio’s built in tools, you’re limited to what they will allow you to do. With the external tools you can do whatever you want that they are capable of doing, which gives you more flexibility in how you can use the tools. Say I’ve done some pretty heavy editing of a stored procedure, and I remember that something I got rid of I need to pull back into my local copy. With one of the external tools I can simply run a compare, pull the section back into my local copy from source control and save the changes right within the tool. With the built in tool I’d probably end up running through a series of steps involving copying and pasting to perform the same type of operation.

I’ve also found that the compare tool built in to Visual Studio 2008 will not compare files if they have different character encodings. This can become inconvenient if you use multiple editors or tools that save code by default in different character encodings – UTF-16 vs UTF-8 for example. Visual Studio 2012 seems to have fixed this issue, but if you’re like me and frequently find yourself working on SQL Server 2008 projects then you’re often working in VS 2008, and switching back and forth between the two can also be inconvenient.

Conclusion

Now that you’ve seen a couple of the alternative options available for comparing and merging your source controlled documents, why don’t you try one out for yourself? Hopefully you will find the use of one of these tools will make your compare and merge work simpler and quicker. Let me know how they work out for you.

Leave a Reply

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