THE SNP has made an explicit commitment to a second independence referendum if Brexit goes ahead after the party establishment saw off a grassroots rebellion.

In a rare public display of internal division, rebels warned against a headlong rush into a new vote while others raised doubts over the economic logic of quitting the UK to stay in the EU.

A motion from the Edinburgh Western branch said “every avenue” should be explored to keep Scotland in the EU, but if there was “no viable solution to safeguard our membership as part of the UK, Scotland should prepare for a second referendum and seek to remain in Europe as an independent country”.

It was passed comfortably, but only after a string of senior party figures, including External Affairs Secretary Fiona Hyslop, Brexit minister Mike Russell and trade and investment spokesperson Tasmina Ahmed-Sheikh encouraged members to back the text.

One SNP member, Robert Martin, said he had proudly voted for Brexit and added: "Many of my fellow Scots also voted leave amongst 17 million others. [The motion] ignores totally the rest of the UK in trading terms and therefore doesn't address the economic risks of disrupting our relationship with the rest of the UK in order to pursue our relationship with a smaller export market in the rest of Europe.

"For those of you arguing we are being dragged away from Europe I just want to remind you of some of the terminology of some of the European presidents who came to give their view on the first independence referendum... They weren't exactly putting the welcome mat out for us, they were trying to slam the door in our faces.

"Let's try to understand and re-examine what our relationship with the EU should be before we hang it round the success or otherwise of a second independence referendum."

Alex Orr, who proposed a 'remit back', a delaying mechanism meaning the motion would have been rejected but reconsidered, said: "I, like you, desire independence with all my being. However, I want to be absolutely sure that when we go for a referendum we win it. I don't want to see us rush headlong, enthusiastically, into a new referendum.

"The idea of a soft Brexit, as far fetched as it might seem, may actually be achieved. We may have a deal secured that proved very popular with the Scottish people and with the EU. Do we then rush forward to have another independence referendum even though the support of the Scottish people may not be with us?

"It would be remiss of me not to flag up the issues this motion outlines. Let's look at it, let's remit it back, and bring it back when we're actually more secure, safer, in what potentially could be achieved for Scotland when that deal is more advanced and we know what's actually on the table for our nation."

Senior SNP figures admitted to being nervous about the result of the vote but blamed a small but vocal band of Eurosceptics for the rebellion.

There was confusion when party veteran Gerry Fisher raised a point of order after a move for a direct negative, a vote to explicitly reject the motion, was dismissed on a technicality.

Mr Fisher's microphone was cut off and he initially refused to leave, with another elderly member twice taking to the stage to protest.

Tony Giuliano, a former Holyrood election candidate in Edinburgh Western, who helped write the motion, said: "I want to tell Theresa May now that remain means remain.

"I don't want to wait two months, or three months down the line to give the reassurance to 170,000 EU nationals that this party, and this country, stands by them.

"Sending this motion back today, rejecting this motion, is a slap in the face to providing that reassurance to 170,000 people that want that reassurance now.

"This is an opportunity, and we cannot miss an opportunity, at a time that the bargaining will begin in Brussels and in London. We need to make our position clear, that Scotland will no longer be sidelined, will no longer be an afterthought, on Westminster's agenda.

"Our manifesto made it clear - material change - Europe was one of them. This is a material change and if we do not stay in Europe we have the right to call an independence referendum to protect our national interest."