David Cameron has been spared the embarrassment of a defeat over immigration at the hands of his own rebel MPs, but only after a day of extraordinary political confusion and significant backtracking which saw him accused of presiding over "chaos".

In the end, Cameron chose to rely on Labour votes to defeat a move to challenge human rights laws and allow ministers to deport foreign criminals even if they claimed to have a family life in the UK.

But he still saw almost 100 of his own MPs backing the rebel amendment.

The issue had caused headaches for both party leaders and, at one point, it appeared Ed Miliband would privately give the green light to Labour MPs to support the amendment if they wished, in the belief it would help defeat the government.

The government had always opposed the move, claiming it would breach human rights laws and put the UK in conflict with the Court of Human Rights in Strasbourg.

But in an eleventh hour switch, Cameron told his MPs they could vote how they liked and ordered his ministers to abstain. He even said, through his spokesman, that he had: "a great deal of sympathy with the objectives which are in play here - to deport more people, more quickly."

But some claimed his change of heart only came after it appeared the government was set to suffer a humiliating revolt by backbenchers so he was deliberately backing out of the fight.

In the end 86 Tories still voted for the amendment but, because of the prime minister's about-face, were not officially defying him.

Labour, on the other hand, was forced into the open and had little option but to vote against the amendment which they publicly always claimed would lead to confrontation with the European Court of Human Rights and was illegal.

Miliband will, however, be able to claim that the Commons made the correct decision, thanks to the votes of his MPs, while the government appeared disunited and chaotic.

And that is probably the impression that will remain after the dust has cleared.

Once again, the prime minister will appear to have been held hostage by his Eurosceptic rebels and, as a result, been forced into parliamentary contortions to avoid humiliation.

Many of the backbenchers were part of the same group of 81 who defied him in 2011 by demanding a timetable for a referendum on EU membership. Cameron has since supported the move as a way of taking the sting out of that rebellion.

Ironically, thanks to largely Labour manoeuvring in the House of Lords, that Bill has almost certainly ben killed off and will have to be re-introduced by another Tory MP in the next session of parliament in the summer.

That has already seen Cameron challenged by his rebels to be as good as his word and prove he really backs the timetable by introducing it as a government Bill, which would ensure it was passed.