LOS ANGELES  — The San Francisco Giants rallied in the eighth inning to beat the Los Angeles Dodgers 3-1 on Sunday at Dodger Stadium, avoiding a four-game sweep and keeping alive slim playoff hopes.

Rookie Trevor McDonald delivered a strong first career start, going six-plus innings and giving up just one run, helping keep the Giants in the game until their offense could break through.

  

The Dodgers looked in control for much of the day behind Emmet Sheehan, who struck out 10 over seven innings and allowed just one hit through that stretch.

But in the eighth, San Francisco’s bats came alive. Christian Koss opened the rally with an infield single, Drew Gilbert followed with a hit, and then Patrick Bailey — pinch-hitting — ripped an RBI double to tie the game. Willy Adames forced in the go-ahead run on a bases-loaded walk, and Matt Chapman added an insurance run on a groundout.

  

Los Angeles reliever Blake Treinen, who entered with the lead, faltered in that eighth inning, surrendering the tying and go-ahead runs.

The loss cost the Dodgers some breathing room in the NL West. They remain ahead of the San Diego Padres, but their margin shrank with only a few games left in the regular season. San Francisco, now 77-79, is still mathematically alive in the wild-card race, though trailing by several games.

  

Dodgers manager Dave Roberts also said after the game that Japanese star pitcher Roki Sasaki will join the team in Arizona this week and likely begin pitching out of the bullpen as early as Wednesday.

The Dodgers closed their home schedule with a 52-29 record in front of 46,601 fans. The Giants, meanwhile, finished 39-42 on the road.

 

With the win, the Giants salvaged some pride and delayed elimination, while the Dodgers will look to regroup as they chase down the NL West crown and prepare for the postseason.