You can effectively merge to directories with UnionFS by overlaying the two directories (called branches in this context) into one single mount point.
There are several implementations of UnionFS; UnionFsFuse is one of the simpler ones, since it is an application (rather than a kernel patch).
The website contains a link to their archive. Compiling the source code should be straightforward on most distros.
In Ubuntu, e.g., it suffices to install the package unionfs-fuse by executing the following command:
sudo apt-get install unionfs-fuse
Once installed, you can proceed to overlay the directories. There are a variety of options. I'll briefly explain the most basic ones:
unionfs-fuse foo:bar baz
overlays the contents of the directories foo and bar in the directory baz.1
This overlay will be read-only; any attempt to create or modify a file in baz will result in a Permission denied. error. However, you'll still be able to create and modify the files inside foo and bar themselves.
Collisions are handled in a fixed way: If a file exists in a higher branch (foo), it will have preference over a file of the same name in a lower branch (bar).
union-fuse foo=RW:bar=RO baz
makes foo writable and bar (read-only). Modifying files of foo will be possible, but modifying files of bar won't be. All newly created files in baz will appear in foo.
union-fuse -ocow foo=RW:bar=RO baz
is similar to the last command. The copy on write option (
cow) has the effect that attempting to modify a file of bar inside baz will result in a modified copy of the file in foo (or any other higher-level writable branch).
With the command
union-fuse foo=RW:bar=RW baz
all can be modified. New files will be created in the top branch (foo).
union-fuse foo=RO:bar=RW baz
allows modifying the files of bar inside baz, but no new files can be created.
sudo umount baz
undoes the union mount.
See also: man unionfs-fuse
1 baz must exist before this command is executed. This applies to all further commands as well.