How do I "merge" two text files?

9,785

Solution 1

Use vimdiff if you like vim. Otherwise, diffuse works great as well.

Solution 2

Version control has more information available when it resolves conflicts: it has not only your version and the other guy's version but also the common ancestor, and thus it can do a three-way merge. Here, the common ancestor is the original version of the configuration file in the distribution, or the official version that you last merged with your changes.

Unfortunately neither Ubuntu nor any other major distribution I know of makes it completely seamless to do three-way merges when a configuration file is updated. You can get close, however, with etckeeper. Etckeeper is an add-on for APT, the package management tool used by Debian and derivatives, that manages /etc in a version control system (Bazaar, Darcs, Git, Mercurial); it's been ported to other systems, including Yum in Fedora. I recommend using etckeeper; it's also a great way of keeping track of the changes you make in /etc.

Some programs manage their configuration files with ucf, but that's not something you have control of as a user.

More generally, when you have the ancestor and two versions, you can do a three-way merge with the merge utility shipped with RCS or with diff3 -m from diffutils.

There are also a great many interactive diff and merge programs. Emacs and Vim have interfaces for that, as do most diff viewers.

Solution 3

My personal favorite is kdiff3 - I do not know, if there is a Ubuntu-version for it. According to the homepage it just uses qt.

With that tool you can merge two (or three) files into a new one. Either by choosing a side for each difference, or by manually resolving the conflict.

Share:
9,785

Related videos on Youtube

Steven
Author by

Steven

Updated on September 18, 2022

Comments

  • Steven
    Steven almost 2 years

    I recently upgraded from Ubuntu 12.04 to 12.10 and at one point, it encountered an Apache config file conflict in apache2.conf. I didn't give me a merge option at that point, so I just rejected the new file and the installer saved the new file as apache2.conf.dpkg-dist.

    I can diff the two files with diff apache2.conf apache2.conf.dpkg-dist and get just the lines that are different. But I want to manually merge the two sort of like how I resolve merge conflicts in SVN or git. How can I do that?

    • daisy
      daisy over 11 years
      Without a proper parser, you can only merge them manually
  • mchid
    mchid about 9 years
    currently (15.04) ubuntu has both kdiff3 and kdiff3-qt