Donald Trump has attempted to come to the aid of a US Marine who had lost his hat, but the president was thwarted by a gust of wind.

Trump was preparing to board Marine One at Andrews Air Force Base in Maryland when he spotted the cap of a marine at the foot of his stairs.

He bent down to pick it up and attempted to reposition it on the head of the stationary marine, but the second he let go, it flew off again in the blustery conditions.

Trump then picked up the hat again, but this time handing it to a military official who was walking with him before boarding his helicopter.

His act of kindness didn't go unnoticed, the former Alaska governor Sarah Palin said: "Thank you President Donald J. Trump."

He was at the air field, which is just outside of Washington, after returning from the G20 summit in Hamburg, Germany.

At the summit, he had a series of high-profile meetings including with the German Chancellor Angela Merkel and the Russian president Vladimir Putin.

In his talks, he failed to come to an agreement over climate change, leaving the US singled out as the only nation not planning to introduce the 2016 accord.

He also discussed election hacking with Putin and announced the creation of a joint cybersecurity unit, but he later stepped back from this.

The pair held bilateral talks for more than two hours before announcing the task force.

The plan had been lauded by Trump's inner circle including Treasury Secretary Steve Mnuchin who described it as a "significant accomplishment."

However, in a tweet, Trump said that despite the announcement, it "doesn't mean I think it can happen."