The U.S. Court of Appeals yesterday overturned a law that automatically committed federal defendants to St. Elizabeths Hospital for the mentally ill when they are found not guilty by reason of insanity.

The court's ruling, which could impact on the upcoming trial of John W. Hinckley, Jr., said that the law was unconstitutional because in all other federal courts such a defendant is released from federal custody unless the government seeks, and wins, a commitment to a mental institution at a separate, noncriminal, hearing.

The two-to-one decision by the appeals court yesterday came in the case of a Tennessee man, Jeffrey I. Cohen, who was arrested in July 1980 near the French embassy in Washington carrying a rifle and two homemade bombs. Cohen was acquitted by reason of insanity of a federal explosives charge and was automatically committed to St. Elizabeths, where he has spent most of the last 18 months.

Hinckley has pleaded not guilty by reason of insanity to charges that he attempted to assassinate President Reagan. His trial has been delayed indefinitely while the Court of Appeals considers what evidence may be admitted at trial.

Hinckley is charged in a grand jury indictment with both federal and D.C crimes in connection with the shooting last March 30 of Reagan, his press secretary, a D.C. police officer and a U.S. Secret Service agent.

If Hinckley is acquitted by reason of insanity of the federal charges, the appeals court decision yesterday apparently would require his release unless the government sought a civil commitment hearing.

At the same time, however, if Hinckley is acquitted by reason of insanity of the local criminal charges, D.C. law contains a specific provision for mandatory commitment to St. Elizabeths. How that conflict between federal and local charges would be resolved in cases such as Hinckley's was unclear yesterday.

The appeals court opinion in the case was written by Judge Abner J. Mikva, who was joined by Judge J. Skelly Wright. Judge George MacKinnon dissented and said he may file a separate statement of his views later.

The decision yesterday may also strengthen the Hinckley defense argument that the prosecution should be required to prove that Hinckley was sane when he shot Reagan and the three others. Trial judges in the Washington federal court have had to decide whether the defense or prosecution should carry the burden of proof on a case by case basis since the law is unclear in this federal district.

Mikva, in his opinion for the appeals court yesterday, noted that in federal courts outside Washington, the government must prove the defendant's sanity beyond a reasonable doubt. The specific queston of the burden of proof was not before the appeals court yesterday. However, if Mikva's analysis on the need for uniform procedures in the federal court on mental commitments were applied to the burden of proof question, it would appear to follow that the Washington federal courts would be required to put the burden on the government to prove sanity.