Answer:
one car at a time
Step-by-step explanation:
For each car in the shorter train* (A) ...
When there are no more train A cars in front of train B, both trains can continue on their journey.
We assume cars can be decoupled at any point in the train, so that any required order of cars can be preserved. We further assume that train B can move any one of train A's cars in addition to all of its own.
_____
* The total number of car lengths that must pass the offshoot is (at least) the product of the number of cars in both trains, so it doesn't seem to matter which train makes use of the offshoot. We choose to decouple the cars of train A so that the minimum number of cycles is required--even though each cycle is longer.