Juan Soto also had three hits and scored three times for the Nationals. Washington's Lucius Fox got his first two hits in the majors after beginning his career 0 for 20.
The Nationals had lost eight straight and were struggling offensively before taking two of three at Oracle Park while scoring 29 runs.
''The biggest thing is pitch selection,'' Hernandez said through an interpreter. ''We had a meeting as a team that we were swinging at very bad pitches in general. The team focused more this weekend and we had better pitch selection and the results were there.''
Josiah Gray (3-2) allowed one hit in six innings, working around four walks while striking out three. The right-hander, part of the trade that sent Max Scherzer and Trea Turner to the Dodgers at the deadline last season, didn't allow a hit until Jason Krizan's two-out single in the fifth.
''Every time he's out there he's getting better, he's getting a little bit more confidence,'' Nationals manager Dave Martinez said. ''You're only seeing the beginning of what he really can do.''
Hernandez, who had seven RBIs through Washington's first 23 games, hit a two-run single in the first inning when the Nationals scored five runs - four unearned - and took advantage of a costly error and Alex Cobb's wildness.
After the Giants scored five times off Washington's bullpen in the seventh to cut the Nationals lead to 8-5, Hernandez lined a three-run double off Jake McGee in the eighth.
''It could have been just been one run, two runs,'' Hernandez said. ''It gave us momentum on our side and we were able to exhale a little bit after that.''
Cobb (1-1), who was activated off the injured list before the game, couldn't get on track. He allowed Hernandez's single, walked three of the next five and was called for a balk with the bases loaded.
Mike Ford, claimed by the Giants off waivers Saturday after being designated for assignment by the Mariners, had a two-run single in the seventh.
Fox's big day came fittingly at San Francisco's waterfront ballpark. The switch-hitting shortstop was originally signed by the Giants as an international free agent in 2015 and was given a $6 million bonus.
''This is where it all started,'' Fox said. ''Signing day I came and watched the Giants play (here). To actually get that first hit in the Giants stadium is special. I had that feeling in my mind that I was going to do something special today.''
Krizan's single off Gray was the first of his career and came in front of his wife and young child, who were in the stands. The rookie outfielder was called up from Triple-A Las Vegas when the Giants placed first baseman Brandon Belt on the COVID-19 list.
Nationals third baseman Maikel Franco took a hit away from Wilmer Flores in the first inning when he chased a grounder into foul territory, leaped and made a strong throw back across his body to first baseman Josh Bell.
Nationals: Stephen Strasburg and Joe Ross will both face hitters this week at the team's facilities in West Palm Beach, Florida, an important step in the rehabilitation for both right-handers. Strasburg, who has made seven starts since signing a $245 million, seven-year contract after being the World Series MVP in 2019, is trying to work his way back from thoracic outlet surgery. Ross underwent surgery to remove bone spurs in his right elbow in March.
Giants: Cobb missed 12 games with an adductor strain. RHP Kervin Castro was optioned to Triple-A Sacramento to make room.
Nationals: RHP Erick Fedde (1-2, 6.00 ERA) starts against the Rockies for the first time since 2018 in the opener of a three-game series in Colorado on Tuesday.
Giants: Carlos Rodon (3-0, 1.17) faces the Dodgers and Julio Urias (1-1, 2.50) in a matchup of left-handers Tuesday in Los Angeles.
More AP MLB: https://apnews.com/tag/MLB and https://twitter.com/AP-Sports
Copyright 2023 STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.