The Gills extended their unbeaten run at Priestfield to four games, three of them wins, with this victory but they certainly made hard work of it.
Having controlled most of the first-half and going two goals up, with young midfielder Jack Payne immense, they were made to struggle after the break once Adebayo Akinfenwa had departed through injury.
They were forced to hang on in the closing stages after Paul Simpson's side pulled one goal back, but hang-on they did and the morale-boosting victory, following last weekend's debacle, took them up to 16th in the League Two table.
The Gills manager made four changes to the side beaten at Accrington, Alan Julian, John Nutter, Tony Sinclair and Kevin Maher replacing Lance Cronin, Josh Gowling, Danny Spiller and Mark Bentley.
The first contentious moment came within the first sixty seconds, a John Nutter headed clearance helped to send Cody McDonald through, via Adebayo Akinfenwa, and he was tripped by Danny Swailes right on the edge of the area. The County skipper getting an early yellow card for his efforts.
Although Chris Palmer's free-kick was deflected behind, the Gills got the perfect start that they craved from the resultant third minute corner-kick.
Palmer swung in the corner from the left and McDonald stole in between two defenders to plant a firm header past the static Matt Glennon and in off the post.
County did get a sniff of goal in the ninth minute when Adam Griffin got through but the alert Julian raced out to hack clear.
The Gills went very close to adding a second in the 21st minute from another Palmer corner from the left. This time Akinfenwa rose above his marker to power a header goalwards only for David Poole to unfortunately be in the right place on the line to clear.
A mis-placed header from Sinclair saw another rare sortee from the visitors, Jake Simpson given the room to break down the left, after cutting inside his low shot forced Julian into a low save to his right.
Julian was forced into action again in the 35th minute when Robbie Williams hoisted a free-kick into the area and Sinclair, under pressure from Mansour Assoumani, made his keeper work with a downward header.
The Gills deservedly went two up in the 38th minute. Palmer played a great cross in from the right which McDonald chested forward. Glennon got his hand to the ball but only knocked it down into the path of Payne who calmly slotted home. It was the youngsters first senior goal for the club.
Referee Graham Horwood then limped off to be replaced by Tim Robinson and his first involvement was a booking for Anthony Pulis, son of ex-Gills boss Tony, for a nasty challenge on Akinfenwa.
From the free-kick, once again expertly played in by Palmer, Akinfenwa put a header wide.
H/T: 2 - 0
The Gills were dealt a blow in the 52nd minute when the hardworking Akinfenwa's troublesome groin forced him to be replaced by Mark McCammon, one of five attacking options on the bench.
Two minutes later Matt Lawrence went down following a challenge with Tom Fisher, a half-time substitute, and he required treatment in the dressing room from the club doctor for a cut above his right-eye.
Despite Lawrence being off for 12 minutes, presumably having stitches inserted, County didn't manage to threaten the Gillingham goal. The only noteworthy event coming as the hour-mark approached, Griffin clattered into Barry Fuller and the replacement official issued his second, and County's third, yellow card of the game.
That increased to four just seconds after the bandaged defender returned as Paul Turnbull was booked for dissent.
Second-half chances for the Gills were rare, especially after McDonald was withdrawn in the 76th minute. Palmer curling a free-kick over the top and as time ran down Rooney, on for Weston, drilled a low shot at Glennon and then, when Dennis Oli blocked the keepers attempted clearance, the forward could only poke the loose ball wide.
At the other end, Lawrence was robbed by George Donnelly who closed in on goal but was denied by a fine Julian save but it wasn't long before County set up a nervy finale by
pulling a goal back with nine minutes remaining. Fisher fed the ball into Donnelly, the on-loan Plymouth striker turned at the near post and perfectly set up the advancing Pulis to stroke the ball home.
The fourth official (Gills kitman Malcolm Stedman) indicated five minutes of additional time and Julian went into the book for time-wasting as the home side looked to run down the clock.
Thankfully, despite pressure from County, they failed to really test Julian in the closing stages and the final whistle brought cheers of relief from around the ground.
Gillingham: Julian, Nutter, Sinclair, Lawrence, Fuller, Palmer, J.Payne, Weston (Rooney 78), Maher, Akinfenwa (McCammon 52), McDonald (Oli 76).
Unused subs: Cronin, S.Payne, Essam, Inkango.
Stockport County: Glennon, Assoumani, Lynch (Pilkington 71), Williams, Swailes, Griffin, Turnbull, Simpson, Donnelly, Poole (Fisher H/T), Pulis.
Unused subs: Vincent, Tansey, Mainwaring, Rose, fon Williams.
Att: 4755 (138 County)
Referee: Graham Horwood
Assistant Referee: Steve Cook & Tim Robinson
Fourth Official: Oleksandr Saliy