The leak was reported at 10:39 a.m. near 24th and Bryant streets by an off-duty firefighter, fire spokeswoman Mindy Talmadge said.
About 40 people were evacuated from buildings in the immediate area, fire officials said. People at other businesses and homes nearby were told to shelter in place.
PG&E spokesman Jason King said a contractor doing excavation work for an ongoing city sewer project dropped a large rock onto a section of pipeline where three lines come together to form a T-intersection.
That meant PG&E crews had to excavate pipeline at three different points in order to squeeze off the flow of gas, he said.
Two of the pipes were capped by about 11:45 a.m., but because the third pipe is made of cast iron, it took longer to close off, he said.
The leak was stopped and the evacuation order lifted around 12:40 p.m.
Gas service was shut off to 10 customers to allow crews to make final repairs to the pipes, PG&E spokesman Joe Molica said.
The repairs are expected to wrap up by about 2:30 p.m., he said. Crews will perform inspections and relight each affected customer's pilot light.
Molica said the utility will be billing the contractor for costs associated with the incident.
PG&E crews will try to mitigate traffic impacts during repairs this afternoon, Molica said.
San Francisco Municipal Railway's 48-Quintara bus line was rerouted because of the incident, Muni spokesman Paul Rose said.
One of the people affected by the evacuation was Allison Gray, a barista at the Sugarlump cafe at 2862 24th St.
"The smell was just horrible," she said.
Gray said she and several customers had to leave the building.
"It's just kind of a hassle," she said.