The 23-year-old, that obtained the 2018 World Cup with France, will combine with Bundesliga leaders on 1 July.
Throughout his clinical in Bayern, doctors ascertained Hernandez had damaged a ligament in his knee and then he subsequently had surgery on Wednesday.
“This is a really important day in my own career,” Hernandez said.
He has won 15 international caps for France, including in all their World Cup games in Russia.
“I am happy to be in a position to join one of the better defenders in the entire world, a World Cup winner, in Lucas Hernandez,” said Bayern sporting director Hasan Salihamidzic.
“Lucas will continue our great tradition of French footballers and better our team.”