Phil Jevons scored a first half hat trick as City progressed to round three of the FA Cup where they will face Coventry City at Ashton Gate.
City were three up and then 4 - 1 up before Gillingham got back to 4 - 3 before the whistle went.
Gary Johnson said of his hat trick hero
"He did well for me when I was at Yeovil and I've always had faith in the lad.
"He took his goals superbly and will get 20-plus again this season providing he stays fit."