Two days after being acquired from Miami in a trade meant to boost the Dodgers' offense and playoff hopes, the slugging infielder went hitless in his first four at-bats against the Giants before homering to left-center on an 0-1 pitch from reliever Sergio Romo (3-2).
Andre Ethier, who walked ahead of Ramirez's home run, had two hits and scored twice to help pull Los Angeles within two games of first-place San Francisco. James Loney and Juan Rivera also had two hits apiece for the Dodgers.
The Giants, looking to bolster their light-hitting offense, obtained veteran infielder Marco Scutaro from Colorado in a deal that was announced during the game.
Ramirez's 15th home run of the season extended a stellar start to his Los Angeles career. He had three hits in the first two games with his new club and is batting .364 since the trade from the Marlins.
The Dodgers slugger also made a potential run-saving play in the field when he dove into foul territory to catch a throw from rookie pitcher Stephen Fife for a forceout following Matt Cain's attempted sacrifice bunt in the fourth.
Ramirez had to wait a little bit before celebrating his big blow against the Giants, however.
Kenley Jansen gave up back-to-back singles to Melky Cabrera and Buster Posey to open the bottom of the 10th. Jansen struck out the next two batters and then got Joaquin Arias to fly out for his 20th save.
Shawn Tolleson (1-0) pitched one inning for his first win in the majors.
Los Angeles needed this one after getting shut out in three consecutive games here earlier this season.
The Dodgers got a stellar outing from rookie starter Stephen Fife and just enough from the bullpen until Ramirez's heroics in the 10th.
Fife was called up from the minors before the game to make his second career start and pitched into the seventh inning. Fife also helped end the Dodgers' 38-inning scoreless streak at AT&T Park when he doubled off Cain in the fifth for his first major league hit.
Jerry Hairston Jr. followed with an RBI double and scored on Mark Ellis' single to give Los Angeles a 2-1 lead.
Before that the Dodgers had not scored at the Giants' waterfront ballpark since Sept. 11, 2011, including a series sweep earlier this season.
Los Angeles got to Cain again in the sixth when Ethier hit a leadoff double and scored on Loney's single.
Cain allowed three runs and nine hits over seven innings. The All-Star right-hander, who struck out two and didn't walk anyone, has only one win over his last six starts and was on the hook for the loss until San Francisco scored twice in the eighth.
Cabrera singled and went to third on Angel Pagan's one-out double. Brandon Belt followed with his third hit, a looping single to center that drove in two and tied the game at 3.
The Giants' biggest problem isn't their pitching, however. That's why they made the deal for Scutaro. He was hitting .271 with four home runs and 30 RBIs in 95 games for the Rockies.
NOTES: SS Luis Cruz has a 10-game hitting streak for Los Angeles. ... Giants 3B Pablo Sandoval is making progress and could return to the lineup as early as this weekend, according to manager Bruce Bochy. ... Dodgers LHP Ted Lilly threw a bullpen session and will pitch for Class-A Rancho Cucamonga on Sunday before the team decides what to do next with the 36-year-old pitcher. ... San Francisco backup catcher Hector Sanchez (left knee sprain) played seven innings for Triple-A Fresno and might see time at first base before getting activated from the DL. ... The Dodgers placed LHP Scott Elbert (elbow inflammation) on the 15-day DL. The move, which was made to open a roster spot for Fife, is retroactive to July 26. ... A fan ran onto the field and made it to home plate before being taken away by security in the middle of the fifth inning. ... Dodgers RHP Chad Billingsley (5-9) faces LHP Barry Zito (8-6) in Saturday's game, which was originally scheduled as a night game before being changed to 1:05 p.m. for television.
Hanley's First Dodger Homer a Game-winner
LA Dodgers Insider Top Stories
Bricktown Breakdown: Dodgers AAA ProspectsHere's a look at how several of the Dodgers' top AAA prospects have done over the past week for the OKC Dodgers.
LA Dodgers InsiderMonday at 12:35 PM
LAD 9 STL 6: Dodgers Torch Mayers, Win SeriesThe Dodgers closed out their 9-game road trip with a resounding 9-6 win over the hated Cardinals and gained on the even more hated Giants in the process.
LA Dodgers InsiderSunday at 9:30 PM
LAD 7 STL 2: Offense Confounds Then ReboundsThe Dodgers bounced back from a tough loss thanks to some timely hitting with runners in scoring position.
LA Dodgers InsiderSaturday at 7:32 PM
STL 4 LAD 3: INCONSISTENT OFFENSE PLAGUESThe Dodger offense spit on opportunity after opportunity before falling ice cold in extras before Matt Adams did them in at Busch Stadium.
LA Dodgers InsiderFriday at 11:06 PM
LAD 6 WAS 3: Ginger Jesus > StrasburgThe Dodgers handed Stephen Strasburg his first loss of 2016 on the strength of two homers from Justin Turner.
LA Dodgers InsiderThursday at 1:15 PM