  (Abridged) We build a theoretical model to study the origin of the globular
cluster metallicity bimodality in the hierarchical galaxy assembly scenario,
based on the observed galaxy mass-[O/H] relation and the galaxy stellar mass
function up to z ~4, and on theoretical merger rates. We derive a new galaxy
[Fe/H]-M(star) relation as a function of z, and by assuming that GCs share the
metallicity of their parent galaxy when they form, we populate the merger tree
with GCs. We perform a series of Monte-Carlo simulations of the galaxy
assembly, and study the properties of the final GC population as a function of
galaxy mass, assembly and star formation history, and under different
assumptions for the evolution of the galaxy mass-[Fe/H] relation. The main
results are: 1) The hierarchical clustering scenario naturally predicts a
metallicity bimodality in the galaxy GC population: the metal-rich GCs are
formed in the galaxy main progenitor around z~2, and the metal-poor GCs are
accreted from satellites and formed at z~3-4. 2) The model reproduces the
observed relations for the metallicity of the metal-rich and metal-poor GCs as
a function of galaxy mass. The positions of the metal-poor and metal-rich peaks
depend exclusively on the evolution of the galaxy mass-[Fe/H] relation and the
[O/Fe], both of which can be constrained by this method. We find that the
galaxy [O/Fe] evolves linearly with z from a value of ~0.5 at z~4 to a value of
~0.1 at z=0. 3) Given a galaxy mass, the relative strength of the metal-rich
and metal-poor peaks depends exclusively on the galaxy assembly and star
formation history: galaxies in denser environments and/or early types galaxies
show a larger fraction of metal-poor GCs, while galaxies with a sparse merger
history and/or late type galaxies are dominated by metal-rich GCs. 4) The GC
metallicity bimodality disappears for galaxy masses below M(star)~1e9, and for
z>2.
