A correction has been added to this story.

EMERGING VICTORIOUS from the Republican primary in October, Rep. Peter Durant of Spencer knew what he had to do to win the general election for the Senate seat vacated by Democrat Anne Gobi.

Durant in the primary did well in his hometown of Spencer and the southern half of the district, but he knew he needed to do better in the northern part of the district, particularly vote-rich Gardner, the home of his opponent, Democratic Rep. Jon Zlotnik.

Durant garnered only 270 votes in Gardner in the primary and came in well behind his opponent. But in Tuesday’s general election, Durant’s vote total in Gardner rose to 1,511, still behind Zlotnik (1,869 votes) but a respectable 45 percent of the vote. He lost Worcester and Princeton in similar fashion, but won every other community in the district.

Durant swept to what appeared to be a double-digit victory in an election that offered a glimmer of hope that Republicans can actually win seats on Beacon Hill. An amiable conservative, Durant appealed to the rural, conservative-leaning voters in the central Massachusetts district and did a better job of getting his voters out in a low-turnout special election to find a replacement for Gobi.

“We really outperformed in every town,” Durant said.

A recent data-based analysis of voting patterns in the district showed why Republicans stood a decent chance of flipping a seat long held by a moderate Democrat.

While Zlotnik avoided debates, Durant raised his profile in the district by leading opposition to gun control legislation on Beacon Hill and shining a spotlight on the state’s emergency shelter law, a statute unique to Massachusetts that requires the state to offer shelter to homeless families with children and pregnant women. The state’s shelter system has been overrun by migrants from outside the country, causing costs to skyrocket and forcing Gov. Maura Healey to violate the spirit of the law by moving to cap admission to the system. Durant became a leading critic of the emergency shelter law.

Durant will become the fourth Republican in the 40-member Senate, joining Sens. Ryan Fattman of Sutton, Patrick O’Connor of Weymouth, and Bruce Tarr of Gloucester. The foursome is no threat to the Democrats who control the chamber, but Republicans hope Durant’s election may offer something to build on.

Paul Craney of the conservative Massachusetts Fiscal Alliance said Durant’s victory showed a Republican can win in the right district. Durant’s Senate district  is probably best described as purple, with three Republican lawmakers representing portions of the district and Durant’s wife, Kate Campanale, a former state rep herself and a candidate for lieutenant governor in 2022, hailing originally from Leicester. Craney said another Senate district with similar characteristics is the one held by Sen. Marc Pacheco of Taunton. [An earlier version of this story mistakenly placed Campanale from Leominster.]

Durant said his victory showed reports of the GOP’s death are greatly exaggerated. But he said it takes all Republicans pulling together to pull out a victory.

“The GOP is a viable force in Massachusetts. We can win seats. We have good ideas,” he said.