How do we know which elements are in a galaxy spectra? I know that each element has it's own distinct spectra (this is a good site that lists them: Periodic Table) but when looking at a spectra of a galaxy (or even a star) how do we match those elements to their distinct spectra? Looking at this image from Spectra in the Lab : 



Some of the lines in the spectra overlap. The galaxies are also moving away, so it makes it more difficult to figure out the elements producing the emission/absprtion lines, right? 

Also, each element usually has more than one absorption/emission line, so how can we say that just one line is the H-alpha line, or the K and H calcium lines, for example, as in this spectra: 



(image from: http://spiff.rit.edu/classes/phys301/lectures/doppler/doppler.html)
Because the wavelength ratio of the lines remains constant despite any cosmological red shift.

For example, if the redshift is $z$, all the lines are shifted redward in wavelength by a factor $(1+z)$. This means that a pattern of lines can still be recognisable.

We also have a pretty good idea of what the spectra should look like, which chemical elements will produce visible absorption features with what relative strengths and so on (see below). This usually makes identification of line features straightforward.

Of course if there were just a single line visible in the spectrum (it does happen, usually in high redshift quasars) it can be difficult to pinpoint the redshift.

In terms of analysing what's in a Galaxy, well usually the light is dominated by the mixed spectrum of billions of stars. This spectrum is interpreted and modelled using galaxy evolution models and population synthesis models that predict a spectrum from a given ensemble.

If a Galaxy is near enough, the chemical abundances of its interstellar medium can be estimated from resolved spectra of emission nebulae.

In comparison, interpreting spectra from an individual star is trivial. Hundreds if not thousands of absorption lines can easily be identified and matched with the predictions of very detailed stellar atmosphere models to estimate chemical abundances. These models contain up to millions of possible radiative transitions as well as the various line strengths and broadening processes that affect the spectrum.