French supermarkets will be banned from throwing away or destroying unsold food and must instead donate it to charities or for animal feed, under a law set to crack down on food waste.

The French national assembly voted unanimously to pass the legislation as France battles an epidemic of wasted food that has highlighted the divide between giant food firms and people who are struggling to eat.

As MPs united in a rare cross-party consensus, the centre-right deputy Yves Jégo told parliament: “There’s an absolute urgency – charities are desperate for food. The most moving part of this law is that it opens us up to others who are suffering.”

Supermarkets will be barred from deliberately spoiling unsold food so it cannot be eaten. Those with a footprint of 4,305 sq ft (400 sq m) or more will have to sign contracts with charities by July next year or face penalties including fines of up to €75,000 (£53,000) or two years in jail.

“It’s scandalous to see bleach being poured into supermarket dustbins along with edible foods,” said the Socialist deputy Guillaume Garot, a former food minister who proposed the bill.

In recent years, French media have highlighted how poor families, students, unemployed or homeless people often stealthily forage in supermarket bins at night to feed themselves, able to survive on edible products which had been thrown out just as their best-before dates approached.

But some supermarkets doused binned food in bleach to prevent potential food-poisoning by eating food from bins. Other supermarkets deliberately binned food in locked warehouses for collection by refuse trucks to stop scavengers.

The practice of foraging in supermarket bins is not without risk – some people picking through rotten fruit and rubbish to reach yoghurts, cheese platters or readymade pizzas have been stopped by police and faced criminal action for theft. In 2011, a 59-year-old father of six working for the minimum wage at a Monoprix supermarket in Marseille almost lost his job after a colleague called security when they saw him pick six melons and two lettuces out of a bin.

It's scandalous to see bleach being poured into supermarket dustbins along with edible food Guillaume Garot, former food minister

Pressure groups, recycling commandos and direct action foraging movements have been highlighting the issue of waste in France. Members of the Gars’pilleurs, an action group founded in Lyon, don gardening gloves to remove food from supermarket bins at night and redistribute it on the streets the next morning to raise awareness about waste, poverty and food distribution.

The group and four others issued a statement earlier this year warning that simply obliging supermarket giants to pass unsold food to charities could give a “false and dangerous idea of a magic solution” to food waste. They said it would create an illusion that supermarkets had done their bit, while failing to address the wider issue of overproduction in the food industry as well as the wastage in food distribution chains.

The law will also introduce an education programme about food waste in schools and businesses. It follows a measure in February to remove the best-before dates on fresh foods.

The measures are part of wider drive to halve the amount of food waste in France by 2025. According to official estimates, the average French person throws out 20kg-30kg of food a year – 7kg of which is still in its wrapping. The combined national cost of this is up to €20bn.

Of the 7.1m tonnes of food wasted in France each year, 67% is binned by consumers, 15% by restaurants and 11% by shops. Each year 1.3bn tonnes of food are wasted worldwide.

The Fédération du Commerce et de la Distribution, which represents big supermarkets, criticised the plan. “The law is wrong in both target and intent, given the big stores represent only 5% of food waste but have these new obligations,” said Jacques Creyssel, head of the organisation. “They are already the pre-eminent food donors, with more than 4,500 stores having signed agreements with aid groups.”

The logistics of the law must also not put an unfair burden on charities, with the unsold food given to them in a way that is ready to use, a parliamentary report has stipulated. It must not be up to charities to have to sift through the waste to set aside squashed fruit or food that had gone off. Supermarkets have said that charities must now also be properly equipped with fridges and trucks to be able to handle the food donations.

The French law goes further than the UK, where the government has a voluntary agreement with the grocery and retail sector to cut both food and packaging waste in the supply chain, but does not believe in mandatory targets.

A report earlier this year showed that in the UK, households threw away 7m tonnes of food in 2012, enough to fill London’s Wembley stadium nine times over. Avoidable household food waste in the UK is associated with 17m tonnes of CO2 emissions annually.