Phil Jevons fired a first-half hat-trick as Bristol City booked their place in the FA Cup third round. The striker curled home Cole Skuse's stunning pass to open the scoring and exploited a hesitant defence to add two in two minutes before the break.
The Gills pulled one back through Gary Mulligan's volley, but Enoch Showunmi's powerful effort made it 4-1.
Michael Flynn headed in moments later and then slotted in a penalty at the death to make it a nervous finish.
Bristol City: Basso, Keogh, Carey, McCombe, McAllister (Fontaine 76), Skuse (Murray 50), Russell, Johnson, Myrie-Williams, Jevons (Brown 84), Showunmi.
Subs Not Used: Weale, Orr.
Goals: Jevons 21, 44, 45, Showunmi 64.
Gillingham: Jack, Jupp, Johnson, Sancho, Easton, Bentley, Crofts, Flynn, Jarvis, Mulligan, McDonald (Ndumbu-Nsungu 46).
Subs Not Used: Jackman, Clohessy, Spiller, Savage.
Booked: Easton.
Goals: Mulligan 49, Flynn 66, 90 pen.
Att: 5,663
Ref: S Mathieson (Cheshire).