WINSTON-SALEM, N.C., May 11— A Federal undercover agent has testified that he knew before the shooting deaths of five anti-Klan demonstrators in 1979 that Ku Klux Klansmen and members of the American Nazi Party unit he had infiltrated would confront the demonstrators.

In previous testimony, the Nazis have said the agent encouraged them to take guns to the anti-Klan demonstration in Greensboro on Nov. 3, 1979.

The testimony Thursday and Friday by the undercover agent, Bernard Butkovich of the Bureau of Alcohol, Tobacco and Firearms, came in a $48 million civil suit accusing law-enforcement officials of knowing that Klansmen and Nazis would use violence to disrupt the demonstration by Communist labor organizers and black residents of Greensboro but deliberately failed to protect them.

Mr. Butkovich is one of four Federal agents named as defendants in the suit, which was brought by people injured in the shootings and the families of those killed. Two previous criminal trials, one a state murder trial and the other a Federal civil rights action, did not investigate the actions of Federal agents or the Greensboro police.

Two Previous Acquittals

The current suit, which follows two acquittals of Klansmen and Nazis, also names as defendants 36 Greensboro police and municipal officials and 20 Klansmen and members of the American Nazi Party.

Mr. Butkovich said he infiltrated the Nazi unit about three months before the Greensboro demonstration.

According to previous testimony of two members of the Nazi group, Raeford Milano Caudle and Roland Wayne Wood, Mr. Butkovich told them he would train them in hand-to-hand combat and the use of explosives and would help them to convert their guns to automatic weapons.

Mr. Wood testified this week that Mr. Butkovich visited him the evening before the demonstration and encouraged him to take a gun to the confrontation in Greensboro. Mr. Wood, whom Mr. Butkovich said he knew to be a ''dangerous person,'' was filmed by a local television station firing successive shotgun blasts at the demonstrators.

Mr. Butkovich characterized his role as an undercover agent as one that gave people with a known propensity for illegal activity the ''opportunity to violate the law.''

Testimony on Pipe Bombs

Mr. Butkovich's superior, Robert F. Dukes, testified that Mr. Butkovich ''was certainly authorized to talk our target into producing guns.''

Mr. Butkovich testified that at a planning meeting Nov. 1, 1979, two days before the Greensboro demonstration, he heard a Klansman say that pipe bombs he had made ''would work good thrown into a crowd of niggers.''

Judge Robert R. Merhige asked Mr. Butkovich at that point, ''You just didn't walk away from that, I presume?''

The agent said he had not made any further inquiries into the statements.

The reference to pipe bombs and possible assaults to which Mr. Butkovich testified was not mentioned in three Federal investigative reports on the shootings.

Conflict on Tape Recording

Mr. Butkovich said that he and his superiors decided he would not join the caravan of vehicles that went to Greensboro to confront the demonstrators, but he conceded he could have been ''in the vicinity of Greensboro'' when the shootings occurred.

The plaintiffs contend that Mr. Butkovich made a tape recording of a meeting in September 1979 at which Klansmen and Nazis formed a group called the United Racist Front, and that the tapes would disclose his role as an agent provocateur and his failure to act on indications that violence would occur.

Mr. Butkovich said the tapes ended abruptly before the meeting started because he did not have an opportunity to change batteries in his body microphone. This contradicted testimony he gave in May 1982, when he said he did change the batteries.