Before gunpowder, one of the common ways to break a city is to break the gates. Since they have to be opened and closed they are intrinsically weaker than the wall itself. 

There were various defences in place: Portcullises that could be dropped either in front or behind the main gate, a double passage that allowed materials like molten lead, or smoking hot oil to be dropped on the enemy. Overhangs, and wall projections that allowed the defenders to make life unpleasant for the attackers.

But once the exterior forces were able to get a decent ram up to the gate, the game seems to have been over.

The following possibility occurs to me:

Construct the wall around the gate with a serious notch on the inside of the passage through the wall. 

Keep a set of timbers that fit this notch, that can be laid horizontally to span the space between the notches.

Keep a reserve of dry earth, sand, or gravel sufficient to fill the space between the timbers and the gate.

One it's obvious that the enemy will get a ram near the gate, put the timbers in place and fill.

Filling could be made faster if the fill was located at the same level or higher than the top of the gate. This would allow wheelbarrows to be used

This would not be done casually, as taking it apart after would be time consuming.

I think this would have roughly the same strength as a stone face and rubble wall. The ram instead of breaking the gate timbers would half to grind the timbers to splinters. With each thump of the ram, the fill would settle and be a better backing.

Is this workable in a pre-gunpowder, muscle and water power world?