The Senate Finance Committee gave unanimous approval to a radical rewrite of the federal tax code early today, bolstered by a trans-oceanic endorsement from President Reagan.

The legislation would cut the top personal tax rate almost in half, to 27 percent, and the corporate rate by a third, to 33 percent, while wiping out numerous deductions and credits.

Approval on the 20-to-0 vote occurred after hours of back-room negotiations that produced a broad package of compromises, including the watering-down of a key anti-tax-shelter provision in order to make it less harmful to the oil and gas industry.

"Something came together, some chemistry," said exhausted and elated committee Chairman Bob Packwood (R-Ore.). "If you don't believe in miracles, you're not a realist."

He and ranking committee Democrat Russell B. Long (La.) predicted the measure would pass the Senate "overwhelmingly."

The dispute over the tax-shelter plan nearly split the committee, which had taken painful votes earlier in the day to ensure that the package would not be eroded by amendments.

Two attempts by Sen. Bill Bradley (D-N.J.) to broaden the tax-shelter provision to cover more oil investments were defeated, 14 to 6 and 11 to 9, after bitter debate in which Bradley said the bill's provision "will be a magnet to attract tax-shelter dollars to the oil and gas business."

The committee also approved last-minute amendments to benefit mining and residential real estate.

After the final vote, Bradley and Packwood bear-hugged to a standing ovation, while downstairs in the Dirksen basement sound-room, lobbyists hissed.

Before the vote, Reagan said in Tokyo that the Senate measure, a sweeping rewrite of the tax code, met his requirements for tax reform.

"I think that very likely I could find myself supporting the Senate committee's version," Reagan said in remarks clearly aimed at reluctant senators. He said their bill was "far superior" to the tax bill the House passed last December, although he had questions about "a few things."

Working in private, Packwood spent almost four hours brokering one final amendment covering the tax-shelter provision and other compromises. It was adopted on a voice vote.

The panel also agreed on a 29-page list of "transition" rules, the little exemptions and exceptions that permit, for example, the Eastern Maine Electric Cooperative to continue to receive tax-exempt financing.

Earlier, committee members had voted down amendments to restore the full deductions for state and local sales and for business meals.

Approval of the bill by the Finance Committee was a remarkable turnaround for a group of senators who, until two weeks ago, showed little enthusiasm for Reagan's top domestic policy initiative.

Committee action, however, does not necessarily mean the Senate will approve. Many senators are openly opposed to repealing popular tax deductions. Even some Finance Committee members plan to offer major amendments on the floor that they held back in committee because of lack of sufficient votes in the 20-member group.

Even if the Senate approves the bulk of the package, it differs substantially from the House version, ensuring yet another hurdle, the marrying of the two versions.

Besides cutting tax rates, the bill would limit such popular deductions as those for Individual Retirement Accounts (IRAs) by making only the interest they earn tax-free, those for consumer interest and those for state and local sales taxes.

The bill would eliminate income taxes for 6 million low-income Americans and provide a tax cut for individuals of 6.2 percent on average. The plan would crack down sharply on tax shelters, and preserve some business deductions while limiting others.

The optimistic tone for the day was set early, when senators agreed in a voice vote that all amendments to the overall package had to be "revenue-neutral" -- that is, they would have to provide for increased taxes on one group to offset the revenue lost if they gave a deduction back to another group.

When Long asked whether amendments would be acceptable if sponsors promised to find the money somewhere before final passage, Packwood said, "I just think the process would absolutely break down if we started down that road."

The committee also approved a compromise on tax-deferred IRAs that would end the deduction for the $2,000 per year or less that workers can deposit to save for retirement, but retain as tax-free the interest earned on those deposits until withdrawal.

The money to "pay" for that change would come from stricter limits on amounts that can be deposited each year in a tax-deferred 401(k) savings plan. The original limit of $7,000 was reduced to $5,000. The vote was 11 to 8.

Earlier, Sen. William V. Roth Jr. (R-Del.) withdrew an amendment that would restore IRAs for all after senators objected strenuously to his proposed method of paying for it: raising the tax on gas 6.6 cents per gallon.

The votes on the deductions for sales taxes and business meals, two politically potent issues, also demonstrated the depth of support for producing tax-overhaul legislation.

Sen. Daniel Patrick Moynihan (D-N.Y.) proposed retaining the deduction for two-thirds of state and local sales taxes. Now, 100 percent of those taxes can be deducted by those who itemize; the committee bill would repeal the deduction for sales taxes while preserving write-offs for state and local real estate taxes, income taxes and personal property taxes.

Moynihan wanted to bring in tax revenue to pay for the cost of his amendment by raising the proposed corporate rate from 33 to 34 percent. Even though the dollars involved balanced out at $12 billion over five years, the proposal was opposed by senators who felt the legislation would fall apart if its tax rates were increased.

Sen. David F. Durenberger (R-Minn.) proposed retaining the deduction for sales taxes and paying for that by raising the proposed individual top rate a half-point to 27.5 percent. He lost on a 13-to-7 vote.

Achieving lower rates was the clear goal of those who joined in opposing new loopholes they might otherwise have supported. Every amendment that would have paid for a change with a higher overall tax rate was defeated.

The 9-to-9 vote defeating a proposal that would have continued to allow the full deduction for business meals illustrated the difficulty in defying restaurateurs and restaurant unions. The bill would limit deductions to 80 percent.