SAN FRANCISCO — Buster Posey struggled in his first few at-bats Saturday night, so he switched to a different bat for his final plate appearance.

The move worked for the Giants' catcher as he had the game-winning hit that gave San Francisco a little more breathing room in the NL West.

Joe Panik hit a game-tying single off Los Angeles closer Kenley Jansen with one out in the 10th inning and scored on Posey's RBI single, and the Giants beat the Dodgers 5-4 on Saturday night.

"It's a big one," Posey said of his fourth career game-ending hit. "I know my numbers aren't great but I feel like I'm still doing a decent job of making pretty consistent contact. I have to tell myself if I consistently square the ball up, that's all I can do."

Adrian Gonzalez homered leading off the 10th to put the Dodgers up 4-3 before the Giants rallied to win with four consecutive hits off Jansen (2-2).

Denard Span started the comeback with a one-out double. Panik singled in Span to tie the game and Brandon Belt followed with a bloop single to right. Posey then lined an 0-1 pitch to left centre as Panik scored easily.

Chris Stratton, the ninth pitcher used by Giants manager Bruce Bochy, retired three batters for his first major league win.

"I put it near the top, if not at the top," Bochy said of the win that moved the division-leading Giants four games up on the Dodgers in the West.

Gonzalez, Justin Turner and Trayce Thompson had two hits apiece for the Dodgers, who lost for the third time in five games.

"Kenley has been great for us and you can't expect him to be perfect," Los Angeles manager Dave Roberts said. "It's a team we're chasing and we had a chance to gain ground."

Both teams were forced to go deep into their bullpens after starters Jeff Samardzija of San Francisco and Scott Kazmir of Los Angeles struggled.

The Giants took a 3-2 lead in the sixth when Panik scored on Matt Duffy's fielder's choice grounder when Los Angeles second baseman Chase Utley's throw to first base was off target.

Thompson, whose older brother Klay was in attendance one day after helping the Golden State Warriors win Game 4 of the NBA Finals, tied it with a two-out, bases-loaded walk in the seventh.

Duffy and Brandon Crawford each singled in runs for San Francisco in the first.

Kazmir pitched into the sixth and allowed three runs for the Dodgers. Samardzija allowed two runs and seven hits in a season-low 4 2/3 innings.

BAT CHANGE

Posey nearly lost his bat in the first inning when it flew out of his hands and stuck in the protective netting behind home plate. The Giants catcher eventually got the bat back and used it three more times before switching to a different model for his final at-bat after lining out in the eighth.

"After the lineout (in the eighth) that one had to take a timeout," Posey said.

STRATTON'S FIRST WIN

Stratton, the last available reliever in the Giants' bullpen, called the first major league win of his career bittersweet after giving up the home run to Gonzalez on an 0-1 pitch. Posey, however, credited the rookie for keeping the score close after left fielder Jarret Parker tried to make a leaping catch on Gonzalez's drive at the fence and came up short. "That inning could have easily spiraled out of control," Posey said. "For him to limit it to one run was really important."

30-YEAR ANNIVERSARY

The Giants held a brief ceremony before the game honouring their 1986 team, which became the ninth team in history to have a winning season after losing 100 or more games the previous year. First baseman Will Clark, second baseman Robby Thompson and outfielder Jeffery Leonard were among the former players in attendance.

TRAINER'S ROOM

Giants: RHP Matt Cain will come off the disabled list and start Monday in the series opener against Milwaukee.... Kelby Tomlinson decided to have a cast put on his left hand to better protect his sprained left thumb. Tomlinson had initially worn a splint. ... RHP Sergio Romo took another step forward in his rehab by throwing off a mound before the game.

UP NEXT

Dodgers: LHP Julio Urias (0-1) will make a fourth attempt to get his first career win in the majors in the series finale Sunday. Urias has pitched just 11 2/3 innings total in his previous three starts.

Giants: RHP Jake Peavy (2-6) has 14 career wins and a lifetime 2.50 ERA in 30 career starts against the Dodgers.