Almost 90 jobs were at risk after bus operator First Scotland East announced it was withdrawing services in East Lothian.
In May 2016, First Group revealed plans to stop bus operations in the area from 14 August 2016.
It also proposed closing its depots in North Berwick and Musselburgh.
The firm had blamed "an increasingly competitive market".
An agreement has now been reached which will see East Coast Buses Ltd, a wholly owned subsidiary of Lothian Buses Ltd, buy the Musselburgh and North Berwick depots with all staff having the option to be transferred.
The buyer has also announced plans to improve services and increase the number of vehicles.
Richard Hall, managing director of Lothian Buses, said: "We are pleased to be in a position to take on and continue providing these vital services and be part of the local economy."
Paul McGowan, managing director of First Scotland East Ltd, said: "I'd like to thank all my colleagues in East Lothian for their hard work and loyalty over the years."
He added: "We will be fully supporting them as they transfer to their new employer."

There are 250 commercial growers in County Armagh, supplying the cider, bakery and fresh markets.
And right now, they are in the middle of a six-week window to get them in.
They are mostly of the Bramley variety, with some eating apples mixed in.
Edmund Allen farms 70 acres of orchard on Grange Lodge farm outside Loughgall.
He aims to produce 1,000 tonnes a year, with about half of it going to make fillings for the bakery trade.
Most of the rest, he sells to cider makers and to shops and supermarkets.
But what is it about apples and Armagh?
Edmund provided his own explanation.
"Apples have been grown here for, we think, around 3,000 years," he said.
"It is the climate, we're near Lough Neagh which acts as a heat sink and keeps the temperature that little bit higher.
"So it's that, the fact that the soil type is good and the experience that we've built up over generations growing apples."
But it can be a precarious business. A sharp frost in early May, killed the blossom and nearly put paid to a year's work.
"I looked out the window and I didn't want to go out near the orchard, I knew what had happened," Edmund added.
"All the blooms I picked off had gone brown, they were dead.
"But we are really truly amazed at the apples that have set, we couldn't find any live blooms and yet nature has healed itself and we have sufficient apples to meet the market requirements."
Some orchards are only returning a 50% harvest due to that frost, but others are up to 90%.
Edmund says prices for his apples have been fairly "stable".
It costs him £40 a bin to produce the fruit. If he can sell them for between £60-80 he is doing well.
But he says he would need £100-110 a bin to reinvest in the business.
His annual running costs are around £80,000 and he tries to replant 10% every five years.
In the old days, there used to be 100 trees to the acre.
Now they are using smaller varieties to get more in and there could be 400 trees in the same space.
And in a another attempt to push up productivity, Edmund is looking to the Netherlands for help.
Native bumblebees caught here are shipped there for breeding, then brought back.
They are being trialled because they are better suited to the climate and are a bit harder working than the hundreds of thousands of honey bees that Edmund relies on to pollinate his trees.

1 July 2016 Last updated at 08:02 BST
The Hitachi Super Express was due to start testing a year ago, but despite being delivered on time, was delayed as the electrification programme to power it is more than three years behind schedule.
Fifty seven trains are now being fitted with diesel engines meaning they can go into service next year on the Paddington line.
The train, built in Japan, made its first run from Reading to London on Thursday.
Threatened East Lothian bus services and jobs have been saved as part of a new takeover package from Lothian Buses Ltd.
[NEXT_CONCEPT]
£20m a year, that is what the harvest is worth to the apple growers of the Orchard County.
[NEXT_CONCEPT]
A new high speed train for the Great Western Railway's services through the Thames Valley has taken to the tracks.