The early Earth would not have been a good place to be hanging out.

First, around 4.5 billion years ago when Earth was just a hot squishy newly formed planet, Theia - an object the size of Mars - slammed into it, sending chunks flying out into space and heating Earth to thousands of degrees.

Then, around 4 billion years ago, the Solar System was enduring a period called the Late Heavy Bombardment, in which asteroids positively pummelled the terrestrial planets of the inner Solar System. Under this attack, Earth remained hot, and its surface molten.

Meanwhile, the Sun, although much dimmer and cooler than it is today, was going through its terrible toddler years, lashing space with violent, powerful flares and a wild solar wind. Under these conditions, it's a wonder Earth managed to retain enough of its atmosphere to evolve into a world hospitable to life.

We may, in fact, have Theia to thank (at least partially). Those chunks of Earth it broke off went on to form the Moon. And new research shows that the Moon's magnetic field could have shielded Earth from the full brunt of the Sun's rage.

"The Moon seems to have presented a substantial protective barrier against the solar wind for the Earth, which was critical to Earth's ability to maintain its atmosphere during this time," said physicist Jim Green, Chief Scientist at NASA and lead author of the new study.

We used to think the Moon was a more-or-less lifeless chunk of rock. It doesn't have a magnetic field now, so we just assumed it never had one, since it's so small and may not have maintained the dynamo effect required.

But when we sent astronauts up there in the 1960s and 1970s, the rocks they brought back showed evidence of magnetism - proof that the Moon once had a magnetic field much like Earth's.

Earth's magnetic field is the result of a dynamo - a rotating, convecting, and electrically conducting fluid that converts kinetic energy into magnetic energy, spinning a magnetic field out into space around the planet. That fluid is Earth's molten iron core.

When it was newly formed, mounting evidence suggests the Moon was also warm enough to have a gooey centre, a molten iron core of its own. Scientists believe it was able to maintain a magnetic field until about 1 to 2.5 billion years ago, when it cooled to the point that the iron core solidified.

"It's like baking a cake: You take it out of the oven, and it's still cooling off," Green said. "The bigger the mass, the longer it takes to cool off."

The Earth-Moon relationship in those early days was a lot closer than how it is today. At about 4 billion years ago, the Moon was just 130,000 kilometres (80,000 miles) away - around a third of its current 384,400-kilometre (238,900-mile) distance. Earth was spinning faster then, too: A day was just five hours. As the planet's rotation slows, the Moon recedes at a rate of about 3.82 centimetres (1.5 inches) a year - it's an ongoing process.

Green and his team wanted to know how the Moon's magnetic field would interact with Earth's under those earlier conditions. So, they designed a computer model to simulate it.

They found that the magnetic fields of the two bodies would be connected via the poles. This combined magnetic field would, at times, have shielded Earth from having its atmosphere stripped by the solar wind.

connectedEarth, the Moon, and their joined magnetic fields. (NASA)

Interestingly, there could also have been some atmosphere exchange. Recently discovered evidence suggests that the Moon had an atmosphere of its own 3.5 to 4 billion years ago due to volcanic activity, held in place by the lunar magnetic field. But nitrogen found in the lunar regolith has puzzled scientists, since it must have been delivered from outside.

The team's simulations suggest that Earth and the Moon could have exchanged atmospheric gases, offering a solution to the puzzle of lunar nitrogen.

Those simulations indicate the two magnetic fields remained joined up until about 3.5 million years ago. It's a very neat finding that matches up with the timing of the lunar atmosphere as well as the strength of the magnetic field, which was previously found to have peaked roughly 4 billion years ago.

The team hopes that new samples obtained in lunar missions will provide more information. Of particular interest are regions in permanent shadow at the poles. They may retain oxygen and nitrogen nicked from Earth's atmosphere that may have been destroyed by harsh solar radiation elsewhere.

This is interesting because it shows the conditions for habitability may rely on a lot more than a certain kind of planet (rocky) at a certain distance (not too hot, not too cold) orbiting a certain kind of star. Other recent research suggests that the presence of a gas giant in the same system could be crucial too, because of its gravitational influence on both the planet and potentially hazardous objects in the outer system.

Working out which of Earth's and the Solar System's characteristics have played an important role in habitability will help us narrow down where to look for extraterrestrial life.

But learning about the Moon is important for its own sake, too.

"Understanding the history of the Moon's magnetic field helps us understand not only possible early atmospheres, but how the lunar interior evolved," said astronomer and NASA deputy chief scientist David Draper.

"It tells us about what the Moon's core could have been like - probably a combination of both liquid and solid metal at some point in its history - and that is a very important piece of the puzzle for how the Moon works on the inside."

The research has been published in Science Advances.