Lee has had three separate loan spells with the Gills.
Gillingham have completed the permanent signing of midfielder Olly Lee following his departure from Hearts.
Lee, 29, has had three separate loan spells with the Gills and finished the season with them in Sky Bet League One
Gills boss Steve Evans told the club’s official website: “He has taken his time to think everything through and has met up with several League One managers who were keen to sign him; at times last season he was simply outstanding.
“He went away late last week with his young family for a few days to discuss his various options and I was delighted when he rang me to say he would love to come back to us on a permanent basis.”
Lee, the son of former England and Newcastle midfielder Rob Lee, joined the Gills on loan in 2012 and then towards the end of the 2019/20 season before returning in January and is their first summer signing.
He spent three years at Hearts and has had previous spells at Barnet, Birmingham and Luton.