The H i gas content is a key ingredient in galaxy evolution, the study of which has been limited to moderate cosmological distances for individual galaxies due to the weakness of the hyperfine H i 21-cm transition. Here we present a new approach that allows us to infer the H i gas mass M HI of individual galaxies up to z ≈ 6, based on a direct measurement of the [C ii]-to-H i conversion factor in star-forming galaxies at z 2 using γ-ray burst afterglows. By compiling recent [C ii]-158 µm emission line measurements we quantify the evolution of the H i content in galaxies through cosmic time. We find that the H i mass starts to exceed the stellar mass M at z 1, and increases as a function of redshift. The H i fraction of the total baryonic mass increases from around 20% at z = 0 to about 60% at z ∼ 6. We further uncover a universal relation between the H i gas fraction M HI /M and the gas-phase metallicity, which seems to hold from z ≈ 6 to z = 0. The majority of galaxies at z > 2 are observed to have H i depletion times, t dep,HI = M HI /SFR, less than ≈ 2 Gyr, substantially shorter than for z ∼ 0 galaxies. Finally, we use the [C ii]-to-H i conversion factor to determine the cosmic mass density of H i in galaxies, ρ HI , at three distinct epochs: z ≈ 0, z ≈ 2, and z ∼ 4 − 6. These measurements are consistent with previous estimates based on 21-cm H i observations in the local Universe and with damped Lyman-α absorbers (DLAs) at z 2, suggesting an overall decrease by a factor of ≈ 5 in ρ HI (z) from the end of the reionization epoch to the present.