German rider Marcel Kittel overtook Mark Cavendish right at the line to win the 12th stage of the Tour de France in a sprint finish on Thursday and Chris Froome stayed safely in the main pack to preserve his big lead.

Cavendish moved in front first but Kittel gained ground and just beat him to the line to earn his second stage win in three days, and third of this race.

"As we say in Germany, good things come in three,'' the 25-year-old Kittel said.

"It was close. I don't know what to say. It was a real sprint today, that's why I'm so happy."

It has been a nervy Tour so far and about 20 riders were caught in a crash near the end, with some stuck under the bikes of others as wheels and frames jutted out at all angles.

"It's always like that at the end before a sprint,'' Froome said.

"It's scary for everyone.''

Froome still leads Alejandro Valverde by 3 minutes, 25 seconds and is 3:54 ahead of two-time former champion Alberto Contador.

Cavendish, the Tour's best sprinter two years ago, has had a frustrating race so far.

The British sprinter looked well set to clinch his 25th career stage win of the Tour as his Omega Pharma QuickStep teammate Gert Steegmans got him in a great position to attack. But Cavendish did not have the legs to hold on and Kittel beat him by half a wheel's length.

"I can go back and look over it but he was just faster,'' Cavendish said.

Peter Sagan finished third and still has a comfortable lead as he bids to win the sprinters' green jersey contest.

Sagan has 307 points, and leads Cavendish by 96 points. Greipel is in third place and Kittel is fourth, but both are more than 100 points adrift of Sagan.

Source: AP