October 4th, 2022

Comparing files in Visual Studio

Mads Kristensen
Principal Product Manager

As developers, we often need to compare two files to find the differences. Sometimes, even comparing the content of the clipboard with a file on disk. And again, sometimes comparing our local changes to previous versions from our Git commit history. There is no straightforward way in Visual Studio to do those things today, but in this month’s experiment we’re going to change that.

Image diff view

Various editors and IDEs offer this feature today, but Visual Studio has required the use of extensions such as VS Diff to provide the feature. An interesting thing about the various implementations is that they offer slightly different features and with different UI paradigms. They all perform basic comparison between files rather well, and we think it’s time we figured out how to design this feature for Visual Studio.

A feature request on the Visual Studio Developer Community is also now gaining steam asking for file comparison, so please vote and comment if you agree.

So, with increased interest and multiple viable solutions, what would the right implementation look like for Visual Studio users?

Let’s do an experiment to find out.

The experiment

We’re starting out with an extension to kick off the experiment. We call it File Differ and the first iteration adds basic file diffing capabilities.

Solution Explorer

Here’s are the commands available from the right-click menu in Solution Explorer:

  • Compare two files in Solution Explorer
  • Compare file with another file on disks
  • Compare file with content of clipboard
  • Compare file with its unmodified version

Compare selected files

Select two files in Solution Explorer and right-click to bring up the context menu.

Image multi selection

Then select Selected Files to see them side-by-side in the diff view.

Compare with a file on disk

If you only selected a single file, a file selector prompt will show up to let you select which file on disk to diff against.

Image single selection

Compare with clipboard

If there is text content on the clipboard, you can compare a file with it by selecting File with Clipboard from the context menu.

Code editor

There are also commands specific to the code editor. By right-clicking inside the code editor, you’ll get the following options for diffing:

  • Compare selection with clipboard
  • Compare active file with clipboard
  • Compare active file with saved version
  • Compare active file with file on disk

Image editor

Next steps

If this feature is interesting to you, please install the File Differ extension and take it for a ride. Make sure to share any ideas and bugs on the GitHub issue tracker, and feel free to send pull requests too. The feature request ticket is a suitable place to share your comments and thoughts, so make sure to vote and comment there as well.

Do you like the idea of community experiments like this one? Let us know in the comments below.

Author

Mads Kristensen
Principal Product Manager

Mads Kristensen is a principal program manager on the Visual Studio team and has published over 150 free Visual Studio extensions. He blogs about anything related to Visual Studio and can often be found hosting various shows on the Visual Studio YouTube channel..

31 comments

Discussion is closed. Login to edit/delete existing comments.

  • miliu99

    As we are moving to the web, the feature I miss the most in VS is JSON diff or YAML diff. Is there a plan to add those diff formats?

  • CAK Cy

    I'll comment on something that may seem unrelated to the article but it is to me. Many times we have added extensions developed by Microsoft, worked with them (took them for a ride) loved them and then Microsoft (for one reason or another) instead of making the extension part of the main Visual Studio software, it ditched them, leaving us developers wondering and complaining about Microsoft's decision. I will mention for example the "Custom Document...

    Read more
    • Chuck Ryan

      I strongly agree with this. Over the last decade having the rug pulled out from under us by Microsoft has become the norm over all their products.

      I used to be a skeptic of open-source projects, as there always existed the risk of a project being abandoned, but now those projects have a better record of longevity and responsive support than Microsoft. What puts the open-source projects over the top for me is the ability to...

      Read more
  • Michel Bretschneider

    Nice, so in simple cases I don’t have to leave VS for that.

    Have you considered semantic diffing like the gmaster git client had? Currently SemanticMerge which is shipped with Unity’s Plastic SCM seems to be the only tool on the market that has this capability.

  • Steven Quick

    As others have mentioned beyond compare is the gold standard imo, 3 way merge view with a 4th pane for output that you can modify.

    Anyone doing a git merge or rebase without a proper 3 way merge tool is playing with one arm tied behind their back.

    • Michel Bretschneider

      If beyond compare is the gold standard, SemanticMerge has to be platin, right?

  • Adin Aronson

    Thanks Mad.

    Let me tell you which Compare option is REALLY missing.
    There is no way to compare a currently modified file with a previous version in GIT. You can only compare your file to the “unmodified version” of the file.

    At least with this extension I can open a previous version, Select All, and copy to clipboard and then go back to my currently modified version and “compare to clipboard”

    • Mads KristensenMicrosoft employee Author

      The team is planning to provide this feature in a future update. I agree, it is really missing, but I’m glad you’re able to use the workaround in the extension to unblock you for the time being.

      • Michael Bond

        Rather than just the previous version if you can extend it to any GIT branches so that I can compare the current version to one from a different branch and see the differences.

  • James Foye

    Well I gave up again on migrating to VS 2022, as I could not even save changes to a file. But hey, this looks like a great feature.

    • Oleg TkachenkoMicrosoft employee

      Please report it via Help/Send Feedback/Report a Problem and post feedback ticket link, this is not a known issue.

  • Bill Casey

    This is a tangential, slightly off-topic comment about 'differences'. I am still really bent out of shape about the change of timestamp when loading a branch from GIT. This creates a DIFFERENCE from when I made the commit, which IMHO should PRESERVE not only code changes but WHEN they were made thus creating ZERO differences! Loading historical snapshots of a project's state at a certain time should include a file's timestamp AT...

    Read more
    • Ira Hsu

      Highly agree with this comment! Several times I have loaded code from Git, and then when it comes time to promote up to our test tier, it looks like I need to upload all the files, since they have a newer date than what is on the test server. It’s hard to tell what files have been actually worked on without restoring their actual time/date stamp. Please make this happen!

  • V D · Edited

    Is there a Diff API available to plugins?
    It would be great to use 3rd party diff tools directly in the VS editor and not in a separate window.

    • anonymous

      this comment has been deleted.

    • Mads KristensenMicrosoft employee Author

      The only API available is a command (`Tools.DiffFiles `) your extension can call. That’s what this extension uses too

  • c h

    VS2019 and 2022. How to install for 2019?

  • James M Curran

    A feature I’d really like: The ability to compare two sections of the same file (greatly needed for refactoring code)

    • Michel Bretschneider

      You should look have look SematicMerge (or gmaster if you can find it anywhere) which detects changes like moving a method within a file.

    • Mads KristensenMicrosoft employee Author

      That’s included. You can compare the text selection with what’s on the clipboard