Three years ago, Gee rejected a similar effort by the Obama administration. She ruled at the time that immigrant children generally can't be held longer than 20 days.
More than 2,000 children were separated from their parents by U.S. immigration authorities at the border with Mexico this spring before President Trump reversed course on June 20 amid an international outcry.
The application to change the Flores agreement — named for an El Salvador immigrant who was 15 when the case was brought in 1985 — was "procedurally improper and wholly without merit," said Gee, who was nominated to the court by President Barack Obama.
The Department of Justice was reviewing Monday's ruling and did not say if it would appeal.
"We disagree with the court's ruling declining to amend the Flores Agreement to recognize the current crisis of families making the dangerous and unlawful journey across our southern border," spokesman Devin O'Malley said in a statement.
Families can be detained together if their parents waive their right to release children to the custody of a family member.
Attorney Peter Schey, who represents detained children in the settlement, said President Trump had falsely claimed the settlement required the separation of families.
"Sifting through the government's false narrative, the court clearly found that the Flores settlement has never resulted in the separation of families," Schey said. "President Trump needs to take responsibility for his own misguided policies."
The ruling came the same day the government acknowledged it would not meet a Tuesday deadline set by a San Diego federal judge who ordered detained children under 5 to be reunited with their families.
Justice Department lawyer Sarah Fabian said 54 of the 100 or so infants and toddlers covered by the order would be reunited on time. She said the delay for others was due to a variety of reasons, including that the parents of some of the youngsters had already been deported.
U.S. District Judge Dana Sabraw, a nominee of Republican President George W. Bush, set a July 26 deadline to reunite all the children separated from their parents.
The government had claimed in its filings that Sabraw's ruling trumped the Flores agreement. Gee rejected that argument on several grounds, saying the U.S. was relying on a "tortured interpretation" of the deal.