For some of the planetary atmospheres, it is easy to understand their origin. In the case of the Jovian planets, the last stage of their formation involved the gravitational accumulation of huge amounts of hydroen and other gases, and their atmospheres naturally arose directly from this accumulated material. For Pluto, there is no atmosphere, in the ordinary sense, most of the time, but when it aproaches the Sun, some of the more volatile ices evaporate (or, more accurately, sublime), forming a thin temporary atmosphere of nitrogen and methane. But what about the Terrestrial planets? They presumably formed too slowly to accumulate any gases, taking longer to form than it took the Sun to somehow rid the Solar Nebula of the gases which had originally made up most of its bulk and mass. How did they accumulate gases, if they formed in a region too hot for gases to exist as solids (ices), and formed too slowly to gravitationally attract gases, later on?
The Theory of Cometary Impacts
One possible answer involves cometary impact. This theory is attractive for several reasons. First, comets are made mostly of volatile materials, albeit in a frozen form: water, carbon dioxide, methane, ammonia, and other compounds of carbon, oxygen and nitrogen with each other, and with the hydrogen which made up the bulk of the Solar Nebula. As a result, a sufficiently large cometary bombardment could presumably deliver large amounts of such icy materias to the planetary surfaces, and depending upon how many comets ran into the Terrestrial planets, during the latter stages of their formation, this might explain most, if not all, of their volatile component (atmospheres, and hydrospheres). However, it is difficult to know whether enough comets might have run into the Terrestrial planets during the last stages of their bombardment to explain their atmospheres. The materials running around in the inner Solar System would have been, because of the high temperatures which existed at that time, exclusively rocky and metallic bodies. Even in the asteroid belt, where current temperatures are well below zero degrees Fahrenheit, temperatures could have ranged as high as six to eight hundred Fahrenheit degrees, during the time that microscoic bits of carbon compounds, ice and rocks would have been colliding with each other, and building up the outer planets.
Now, this doesn't mean that comets couldn't have run into the Terrestrial planets at all. Particularly toward the end of their formation, the Jovian planets would have had substantial gravity, and near misses between them and the cometary bodies left over from their formation certainly threw billions of dirty snowballs into the outer reaches of the Solar System, forming the Kuiper Disk, so there is no reason why large numbers of cometary bodies might not have also been thrown into the inner Solar System, adding to the volatile component of the Terrestiral bodies. It is just hard to know, or even estimate, what fraction of the impact features which we see on the ancient surfaces of objects like Mercury and the Moon might have been due to rocky bodies, and what fraction would have been due to icy bodies. In addition, although the detailed composition of some comets appears somewhat similar to the composition of the Terrestrial planets' atmospheres, most comets appear to have differences in the details of their composition (particularly deuterium) which would have led to slightly different compositions for the Terrestrial planet' atmospheres, compared to the ones they actually had. As a result, it is not at all clear whether cometary impacts accounted for a major portion of the original volatile materials inside the forming Terrestrial planets, or just a very minor portion of those materials.
Despite this uncertainty, the cometary impact theory is moderately popular, at the moment, for several reasons. One is the fact that the basic idea is fairly simple, and obvious. Another is the fact that, in 1994, Comet Shoemaker-Levy 9 happened to run into Jupiter. This was a spectacular event, caused by a passage of the comet by Jupiter, a near miss in which the planet's gravity tore the comet into nearly two dozen pieces, and, as they passed the planet, and tried to move into interplanetary space, slowed them down sufficiently that they were forced to fall back into Jupiter, hitting its atmosphere with speeds of the order of 130,000 miles per hour, and producing spectacular explosions in its upper atmosphere.
Hubble Space Telescope composite image of the pieces of Shoemaker-Levy 9
(H. Weaver (JHU), T. Smith (STScI), NASA, apod990814)
Dark spots on right are atmospheric debris produced by the impacts
Time-lapse sequence of photographs showing the gradual fading of the comet's effects.
(H. Hammel (SSI), WFPC2, HST, NASA, apod001105)
These explosions were actually visible from the Earth, and the sooty debris left over after the comets were vaporized and blown to bits covered areas of the upper atmosphere as large as the entire Earth for several weeks afterwards. Such impacts are very rare, even for Jupiter (perhaps once every 10,000 years or so), and the Earth is a much smaller target (both physically, and gravitationally), so they are even rarer here (perhaps once every few millions of years or so), but it did reinforce the idea, in the popular media, that comets could provide the volatile materials which make up our atmosphere and hydrosphere, especially since astronomers interviewed on various late-night talk shows at the time were not at all averse to such striking demonstrations as holding up a glass of water and stating that the contents of the glass were the remains of comets which had struck the Earth at some time in the past.
Of course, although the newsworthiness of this impact doesn't remove the uncertainty as to whether cometary impacts made a significant contribution to the formation of the Earth's atmosphere, more than 4 billion years ago, but it was a striking event (in every sense of the word), and makes the idea more compelling than it might otherwise have been.
The other reason why the cometary impact theory has been in the news recently has to do with some rather odd events in the atmosphere of the Earth. The upper atmosphere of the Earth is continually bombarded by high-energy particles from space (solar wind particles, cosmic rays, and particles ejected from the van Allen radiation belts), and as a result, there is a very faint atmospheric glow, which is easily visible from space, and has been photographed extensively since the dawn of the space age.
Very early on, it was noticed that at various times, small dark spots appear in the photographs, where the glow seems to disappear for periods of ten or twenty minutes, then gradually reappear. When this was noticed, the discoverer of these spots suggested that small cometary objects, with a mass comparable to an ordinary snowball, but a size many tens of meters across (basically a sort of mist of cometary frost filling a region the size of a small building) might be running into the atmosphere, temporarily changing its local composition, and damping out the normal atmospheric glow. Over a period of time, as the water vapor in the impacting objects mixed with the surrounding gases and became appropriately energized by further impacts, the glow returned.
This idea was met with considerable skepticism, for several reasons. One is that there was no guarantee that the dark spots were real. The equipment used to take the photographs was known to have occasional "glitches" in its operation, and single-pixel errors, permanent and temporary, were not unusual in photographs, so it was quite possible that the dark spots were "artifacts", rather than real deviations in the atmospheric behavior. The other reason had to do with the nature of the supposed snowballs. A compact snowball might be able to hold together, during its passage through the inner Solar System, and in fact we run into dozens of such objects every single day. But a haze of frost with the mass of a snowball, and the size of a building, should completely vaporize long before it reached the orbit of the Earth, so it was hard to see how such a thing could even exist near the orbit of the Earth. Worse yet, the number of dark spots per unit area of the atmosphere, multiplied by the size of the Earth, implied that vast numbers of such things were running through the inner Solar System. To the originator of this concept, that was an attractive idea, because it meant that huge amounts of fresh water and other volatile materials could be delivered to the Earth every single day, and over long periods of time, that could easily explain the atmosphere and hydrosphere of the Earth. However, to those who were somewhat dubious, it seemed quite unbelievable that such large numbers of such strange objects could possibly exist, without being detected, in one way or another. As an example, during a meteor shower, such as the Leonid meteor shower, small specks of material not much bigger than a sand grain can be observed hitting the Moon, by producing small flashes of light on its dark side. Even though the proposed snowballs were supposed to be spread out a bit, impacts by objects of that mass, regardless of how they were spread out, would produce very easily visible flashes on the surface of the Moon, flashes which are not observed, implying that the objects do not actually exist.
As a result of these problems, for several decades, this theory was pretty much ignored. However, not long after the impact of Comet Shoemaker-Levy 9 with Jupiter, it was shown, using more modern and, presumably more accurate, satellite observations, that the dark spots are actually real, and this theory was temporarily revived. However, although we still do not understand what is causing the spots, the arguments that strange snow clouds could produce them are still faced with the insuperable arguments against them, already discussed, and virtually no one believes that there is any validity to this theory.
However, despite the uncertainty or even near impossibility associated with these two ideas of cometary impact, their recent prominence in news reports keeps the idea of cometary impacts, as a source of Terrestrial planetary atmospheres, quite alive, and there may be at least some validity to these ideas, particularly for the variation involving impacts by large cometary bodies early in the history of the Solar System, as discussed at the end of this section.
The Theory of Volcanic Outgassing
The alternative theory of atmospheric formation is that gases were somehow trapped within the planets while they were forming, and then released, later on, through volcanic activity. Even nowadays, when fresh magma is brought to the surface of the Earth, from partial melting of upper mantle material, as in the volcanoes on the Hawaiian Islands, and in Iceland, the lavas contain huge amounts of dissolved gases, which provide enough pressure to blow fountains of lava several hundred feet above the surface. Early in the history of the Earth, there must have been even more gases trapped inside it, and when it melted and differentiated, most of its surface must have been molten at some point, and immense amounts of volcanic gases would have been released from its interior.
These gases consist primarily of compounds of hydrogen, which is the most abundant material in the Universe, and in the Solar System, and carbon, nitrogen and oxygen, which are the next most abundant reactive elements (helium is also fairly abundant, but is chemically inert, and doesn't form compounds), and sulfur. Water vapor (H2O) and carbon dioxide (CO2) are by far the most abundant volcanic gases, but sulfur dioxide (SO2), carbon monoxide (CO), hydrogen (H2), hydrogen sulfide (H2S), sulfuric (H2SO4) and hydrochloric acid (HCl) are also fairly abundant in modern-day volcanic gases, and helium (He) and hydrofluoric acid (HF) are not uncommon. In the early stages of Earth's history, nitrogen (N2), methane (CH4) and ammonia (NH3) are also thought to have been released in large amounts. The total amounts of these gases could well have been enormous -- quite possibly several hundred Earth atmospheres -- but the atmosphere wouldn't have remained like that for very long, because ultraviolet radiation from the Sun would have broken down molecules in the upper atmosphere, just as it now does, in the exosphere and upper mesosphere.
As hydrogen was split from its compounds (which represents most of the chemicals mentioned above), the light atoms would have gradually floated to the top of the atmosphere, and been gradually lost. Helium atoms would have also tended to disperse into space, although at a slower rate. Eventually, the major remaining component of the atmosphere would be carbon dioxide, with minor amounts of nitrogen. There would also, at some point, have been various amounts of oxygen and sulfur, but those chemicals are relatively reactive, and would have combined with the surface rocks in various ways, and been removed from the atmosphere. As a result, after a while, the atmospheres of the Terrestrial planets (at least the three that have enough gravity to hold onto gases) would have been very much like the atmosphere of Venus -- primarily carbon dioxide, with trace amounts of nitrogen and other very minor gases. In addition to being the same composition as Venus, they would have also been similarly thick, at least for the Earth, which probably had an atmosphere, at that point, about a hundred times thicker than now, made almost entirely of carbon dioxide. Mars, on the other hand, because of its smaller size, and because it may not have melted and differentiated quite as quickly and thoroughly, might have only had an atmosphere a few times thicker than our present atmosphere, and could have had an atmosphere not thicker than our present atmosphere.
The Evolution of the Terrestrial Planets' Atmospheres
Now, Venus still has an atmosphere just like the one just discussed, but neither the Earth nor Mars have that type of atmosphere. So, if that is the way our atmosphere was formed, why do we have the atmosphere we now have? In a nutshell, because of the presence of liquid water at the surface of the planet.
Because Venus is closer to the Sun than we are, it is considerably hotter (especially so, because of the runaway greenhouse effect which its atmosphere causes), so hot, in fact, that even when the Solar System had just finished forming, and the Sun was a bit fainter than now, the surface temperatures would have been sufficient to boil away any oceans which it might have developed (or, more accurately, to keep water vapor from ever condensing to form an ocean). With all of the water on Venus existing as vapors in its atmosphere, ultraviolet radiation from the Sun would have "soon" broken it down into hydrogen, which would have escaped into space, and oxygen, which would have combined with the surface rocks, leaving only a very minor amount of water (approximately one-quarter of the mass of the clouds, which are made of fuming sulfuric acid).
The Earth, however, is somewhat further, and when the Sun was a little fainter than now, even with the powerful greenhouse effect produced by a thick carbon dioxide atmosphere, would have been too cool to have a runaway greenhouse effect. The surface temperatures would have certainly been higher than the "standard" boiling point of water (212 Fahrenheit degrees, or 100 Celsius degrees), but that is the boiling temperature of water only when you have one Earth atmosphere of pressure. With an atmosphere a hundred times thicker than now, the boiling temperature of water would have been several hundred degrees, and even with a powerful greenhouse effect, water vapor could have, and would have, condensed to form the primitive oceans on the Earth.
With water oceans, the Earth's atmosphere developed very differently, because carbon dioxide can dissolve in water, forming a weak acid, carbonic acid (if you ever took a high school physics or chemistry class, your teacher may have tried to put you off drinking soda pop by leaving teeth or nails in a glass of soda pop overnight, to show how corrosive the material is, despite being a very weak acid). Meanwhile, weathering and erosion of the primitive continents would have been washing various metal oxides (primarily of iron, magnesium and calcium) into the oceans. These metal oxides are technically bases, and when combined with acids, such as carbonic acids, they form materials known as salts. Some such salts are soluble in water, but the salts formed by combining metal oxides with carbonic acid, which are called carbonates, are insoluble, and their solids would have been deposited on the sea floor as iron carbonate (siderite), magnesium carbonate (dolomite), and calcium carbonate (limestone). As the process of continental drift pushed the sea floors and continents together, immense slabs of these carbonate rocks piled up on the continents, until eventually, tens of thousands of cubic miles of what was once carbon dioxide gas was locked up in the surface rocks. As a result, over time, the carbon dioxide atmosphere of the Earth gradually disappeared, leaving only a relatively thin atmosphere of nitrogen and less common gases. (Note: If we wanted to, we could return the atmosphere to its original composition by digging up all those carbonate rocks, heating them up, and driving off the carbon dioxide. It would require a tremendous effort, and a tremendous expense, but it could be done, and if it were done, we would have the same sort of atmosphere we once had, an atmosphere just like that of Venus, and surface temperatures on the Earth would be raised by the greenhouse effects of those gases to several hundred degrees above zero.)
A schematic representation of the probable evolution of the Earth's atmosphere. Very early on (not shown), volcanic outgassing created a very thick atmosphere composed primarily of water vapor, carbon dioxide, and other hydrocarbons. Within a couple of hundred million years, photodissociation of the hydrogen compounds and condensation of the water removed most of the hydrocarbons from the atmosphere, leaving large amounts of carbon dioxide and nitrogen (the residue of photodissociation of hydrogen compounds which involved nitrogen, such as ammonia). During the next few hundred million years, carbon dioxide dissolved in the oceans, combined with metal oxides in the sediments washed into the oceans by weathering and erosion of the early continents, and precipitated out as insoluble carbonates, leaving only nitrogen as the primary component of our atmosphere. Much later, photosynthesis (due to plants and blue-green algae) produced oxygen, as a waste product, adding a variable but mostly gradually increasing amount of that unstable gas to the atmosphere.
A similar thing might have happened on Mars, as well. Nowadays, Mars cannot have liquid water on the surface, except under very rare circumstances, because the atmospheric pressure is so low that at any temperature much above the freezing point, it would simply boil away into the atmosphere. But if Mars once had a carbon dioxide atmosphere as thick as, or even thicker than, our atmosphere, not only would there have been plenty of pressure to keep water in a liquid state, but temperatures would have been increased, by the greenhouse effect of the carbon dioxide, from their present-day values of almost a hundred degrees below zero, to well above the freezing temperature of water. In fact, conditions might have been very similar to present-day Earth-like conditions. It is not possible that Mars can have had such conditions recently, because its surface has large numbers of relatively ancient craters, which indicate that hardly any weathering and erosion, and relatively little geological activity, has taken place in Mars for the best part of four billion years, but it is quite possible that, around or before four billion years ago, Mars might have had very different conditions from today, including oceans and lakes, clouds and rain and rivers, and there is a large minority of astronomers and lay people who would dearly love to believe that this was so, because then the chance that life might have developed on Mars, early in its history, would be substantially enhanced.
Of course, if Mars did once have oceans, they would have been just as capable of dissolving its carbon dioxide atmosphere as the Earth's oceans were, at dissolving and precipitating out the carbon dioxide atmosphere of the Earth, as carbonate rocks. Over time, the atmosphere would have become thinner and thinner, but whereas, in the case of the Earth, the carbon dioxide would have been almost completely removed, because, being closer to the Sun, temperatures could have remained reasonable even as the atmosphere disappeared, at Mars' greater distance from the Sun, the loss of the carbon dioxide would have removed the greenhouse effect that kept it warm, and at some point, the oceans would have frozen, and the removal of carbon dioxide would have come to an end, leaving at least the thin atmosphere that we currently see.
Whether this is actually true is not known. If it is true, then Mars should have large amounts of frozen water either near, or somewhere below, its surface, perhaps even large amounts of frozen water mixed with carbon dioxide, and substantial amounts of carbonate rock, as well. As of now, however, we don't know if Mars contains any carbonate rock, and although there is a substantial suspicion that there are large amounts of permafrost buried beneath the Martian surface, there is no definite evidence to that effect. The river-like features which are found in some areas of Mars are most easily explained as flash flooding caused by a mixture of soil and water rushing downhill after volcanic heating melted part of a permafrost layer, but there are alternative theories of the formation of those features which involve a fluidized mixture of carbon dioxide and rock, instead. It will take further exploration of Mars, and a detailed study of its surface and interior, to establish, beyond any doubt, exactly what has caused the features that we see, and whether Mars once had a much thicker atmosphere than it now does, or substantial amounts of surface water.
Further Evolution of the Atmosphere of the Earth
This is not quite the end of the story, either, for either Mars or the Earth, as the atmosphere of Mars is much thinner than we would expect, even if the above discussion is correct, and the atmosphere of the Earth contains not only large amounts of nitrogen, but also a substantial minority of oxygen.
In the case of the Earth, we of course know the answer to this additional detail. Early in the history of the planet, in fact, well over 3 billion years ago, life had established an extensive foothold in the oceans of the planet. At first, lifeforms on Earth probably used hydrogen sulfide as their primary energy source, in the same way that nowadays, creatures which live near volcanic vents on the ocean floor do. Fairly early on, however, blue-green algae developed the ability to use photosynthesis -- absorbing sunlight, and using its energy to drive chemical reactions which turned carbon dioxide and water vapor into various sugars, leaving, as a waste product, substantial amounts of oxygen. At first, the amount of oxygen would have been relatively minor, which was good for the life that existed on the Earth at that time, because oxygen is a very active and corrosive material, and is toxic to many types of life (even animals, which depend upon oxygen for their energy, suffer irreparable harm to their lungs if the amount of oxygen in the atmosphere is too high). Over time, however, the amount of oxygen gradually grew, and those lifeforms which were most sensitive to its deleterious effects were driven into niches, such as at the bottom of the sea, where the oxygen abundance was close to zero. Meanwhile, other lifeforms developed, such as primitive animals, which were able to tolerate and even take advantage of the energy released when oxygen combines with other materials.
For a long time, the abundance of free oxygen in the atmosphere was well under 10% of the total, and various materials dissolved in the oceans, such as iron compounds, were free to remain in solution. Around two billion years ago, however, the oxygen content rose to such a high amount that oceanic iron began to oxidize, and rusty clays began to precipitate out of the oceans, all over the Earth, producing a characteristic series of reddish sediments referred to as red beds. During the time prior to the formation of these red beds, iron ores often contained substantial amounts of free iron, but since then, iron ores have mostly contained various oxides, such as hematite and limonite.
Even after that, the oxygen content continued to grow. Nowadays, it is over 20% of the total atmosphere, but that is not necessarily the greatest percentage that it has ever had. Prior to a few hundred million years ago, all lifeforms on Earth lived in the oceans, and the land surfaces were essentially bare. At that time, there was no reason why oxygen content couldn't reach values as high as 30%, and in fact, in the Carboniferous era, when life first began to move into swampy areas on the periphery of the continents, immense insects proliferated, including dragonflies with wingspans of as much as six feet. Nowadays, such large insects cannot exist, because insects do not have lungs. They breathe by absorbing oxygen through microscopic passages, or tubules, which allow air to diffuse into their bodies. In the current, 21% oxygen, atmosphere of the Earth, only insects a foot or less in size can get enough air into their deep interiors to maintain proper bodily functions. But if the oxygen content in the Carboniferous had been closer to 30%, insects could have been considerably larger, and still maintained adequate ventilation in their interiors. This does not, of course, prove that the oxygen content was that high at that time, but it does suggest that it might have been.
Soon afterwards, however, plants began to spread across the land surface of the Earth, and a problem developed which would have guaranteed that the oxygen content could no longer approach such high levels -- namely, the possibility of extensive fires. Because oxygen is so chemically reactive, a land surface covered by plants, in the presence of oxygen abundances well in excess of 25%, would have been extremely susceptible to furious firestorms, unlike anything we can now imagine. As a result, since the development of extensive plant life on the continents, oxygen abundance in the atmosphere has probably normally been well below 25%, as it now is.
One thing which might be noted is that some people like to imagine that life in some ways controls the atmosphere of the Earth, in a sort of almost intelligent feedback mechanism, so that the atmosphere, at least when unaffected by human activity, remains more of less stable, and "just right" for life to flourish. This is rubbish. There are substantial interactions between the atmosphere of the Earth, and the lifeforms which inhabit the Earth, and as one of these changes, it affects the other one, as well. But it is just as likely for changes in the atmosphere to produce sudden and drastic changes in the lifeforms which can inhabit the surface of the Earth, as it is for the atmosphere to somehow coddle and nurture the present, completely temporary variety of life. The history of the Earth in particular, and planets in general, involves long periods of time where things tend to remain more or less the same, but there are frequent catastrophic changes of one sort or another, driven by astronomical, atmospheric, geologic and biological changes, and the only thing that is true about the Earth is that change, and dramatic change at that, is as normal as stability.
Further Evolution of the Atmosphere of Mars
As a not quite final note, we return to the atmosphere of Mars. At one time, it was almost certainly a thick combination of various gases, primarily hydrogen compounds, and carbon dioxide. Later, after the hydrogen molecules had been photodissociated (broken down into hydrogen and other gases by the absorption of ultraviolet light), and the hydrogen had been lost, the atmosphere consisted primarily of carbon dioxide, as it does now, but presumably far more carbon dioxide than it currently has. Presuming that, at that time, the greenhouse effect of the carbon dioxide allowed the planet to have much warmer temperatures than now, it might have had extensive oceans, lakes, rain, rivers and erosion, for a short period of time. But the presence of large numbers of craters, many of which must date back the best part of four billion years, implies that this early episode of clement conditions, if it ever existed, must have soon ended. We might well ask, why?
One possibility is that, during this time, the Solar System was still full of large numbers of relatively large objects, bits of rubble left over from the formation of the planets, which were still running into (or being swept up by) them. Perhaps one such object hit Mars a sufficiently violent blow to eject most of its atmosphere into space. Or, as previously stated, perhaps a good part of the carbon dioxide dissolved in the ancient oceans, and as the greenhouse effect of the atmosphere disappeared, the oceans froze, and that process ceased. If so, however, there should still have been more atmosphere left, than there now is. Why is the current atmosphere so thin?
Partly, this is because Mars is so small that it is only barely able to hold onto gases. As long as the gases in its atmosphere remain as whole molecules, which are relatively heavy, they can be held onto for many billions of years. But there is a continual breakdown of the molecules into individual atoms, as a result of the absorption of ultraviolet light, and the individual atoms are a bit lighter than the molecules that were broken off of. As a result, although the process is very slow, it is possible for otherwise permanently stable portions of the atmosphere to gradually escape into space. It would take billions of years to lose significant amounts of gas in this way, but the Solar System is 4.5 billion years old, so Mars could have lost as much as 90% of the atmosphere that was left over after any initial changes, during that time. Whether this is enough of a loss to explain the current, very thin atmosphere, is not at all certain, and most astronomers and planetary geologists believe that there must still be substantial amounts of gases somehow trapped inside the planet, either never having been outgassed to the surface (as a result of incomplete differentiation and volcanic activity), or combination with the rocks (as in the case of carbonates, if they exist), or in some other way. But, as already mentioned, only time will tell which, if any, of these theories are correct.
The Source of Outgassed Atmospheres
Now to consider one last problem. As implied by the bulk of this discussion, we believe that the atmospheres of the Terrestrial planets, or at least the original atmospheres, prior to any subsequent evolutionary changes, must have originated from outgassing (volcanic emissions) when they melted and differentiated, not long after their formation. But where did the gases come from, in the first place?
One possibility has to do with the fact that the planets built up out of solid bits of rocky materials (metal oxides of various sorts), rather than icy or sooty compounds, because they were formed in a very hot part of the Solar Nebula, with temperatures, for all four Terrestrial planets, near or well in excess of a thousand Fahrenheit degrees. Although that would seem to rule out the possibility of any gaseous materials being trapped inside them, there are two ways in which at least small amounts of gases could be trapped. One has to do with the fact that the rocky materials would have been running around in huge amounts of hydrogen and other gases, and at the high temperatures involved, would have been undergoing continual chemical reactions with those gases. Many of the compounds that resulted would have been unstable, at the high temperatures, and boiled away as quickly as they were formed, but at any given time, there must have been some small amount of temporary compounds, and large amounts of other gases, on the surface of, and between, the various solid grains which built up to form the planets. As those grains collided, and began to grow, from microscopic bits to sand and pebble and rock size bits, very tiny amounts of various gases would have been trapped between the colliding objects. Whether this would be adequate to explain the huge amounts of gases which were apparently trapped inside the planets, prior to their melting and differentiation, is very uncertain, but it would have at least provided a portion of their original gases.
The other possibility is that, during the same time that the Terrestrial planets were beginning to near their final sizes, and start to melt, the Jovian planets, which formed faster, because they were forming in a region with far more solid material (the lower temperatures in the outer Solar System allowing not only rarer rocky materials, but also much more common carbon compounds and ices to exist as solids), were already nearing sizes which would allow them to gravitationally attract tens or hundreds of Earth masses of hydrogen and other gases. As they mushroomed in size, the gravities of the Jovian planets would have become immense, and as they began to sweep up not only huge amounts of gases, but also large amounts of dirty snowballs which had not yet been incorporated in their structure, they (particularly Jupiter) would have begun to toss dirty snowballs which passed too far from them to actually run into them into other parts of the Solar System.
Some of those snowballs (in fact, tens of billions of them) would have been tossed far out of the Solar System, into the nascent Kuiper Disk, but many of them would have been tossed into the inner Solar System, and had at least a chance of running into the Terrestrial planets, and provided them with large amounts of volatile materials. Whether this idea is theoretically tenable is not entirely clear, but since there are those who would like to believe that comets are the actual source of our water and other volatile materials, this idea is considered, by them, to be very attractive. So, we may have gotten our atmosphere and hydrosphere from comets, after all, but during the time that we were forming, instead of later on. Or, we might not have. We need far more information, some of which may never be available, in order to know the true answer to this question.