Spiral, fast-rotating galaxies like the Milky Way are the most common type in the Universe. One of the most pressing challenges faced by current models of galaxy formation is the origin of their angular momentum and disk. According to the standard tidal-torque theory the galactic spin is originated by tidal interactions between dark halos around galaxies and neighboring structures in the expanding Universe. We use a large cosmological N-body simulation to study the origin of possible correlations between the merging history and spin of cold dark matter halos. In particular, we examine claims that remnants of major mergers tend to have higher-than-average spins, and find that the effect is driven largely by unrelaxed systems: equilibrium dark matter halos show no significant correlation between spin and merging history. Out-of-equilibrium halos have, on average, higher spin than relaxed systems, suggesting that the virialization process leads to a net decrease in the value of the spin parameter. We present also high-resolution N-body/SPH cosmological simulations including cold gas and dark matter to investigate the processes by which gas loses its angular momentum during the protogalactic collapse phase, leading to simulated disk galaxies that are too compact with respect to the observations. We show that the gas and the dark matter have similar specific angular momenta until a merger event occurs at redshift 2. All the gas involved in the merger loses a substantial fraction of its specific angular momentum due to tidal torques and falls quickly into the center. Dynamical friction by small infalling substructures plays a minor role, in contrast to previous claims.