Title: Flipping spins in mass transferring binaries and origin of spin-orbit misalignment in binary black holes Abstract: Close stellar binaries are prone to undergo a phase of stable mass transfer in which a star loses mass to its companion. Assuming that the donor star loses mass along the instantaneous interstellar axis, we derive the orbit-averaged equations of motion describing the evolution of the donor rotational angular momentum vector (spin) which accompanies the transfer of mass. We consider: (i) a model in which the mass transfer rate is constant within each orbit and (ii) a phase-dependent rate in which all mass per orbit is lost at periapsis. In both cases, we find that the ejection of $\gtrsim 30$ per cent of the donor's initial mass causes its spin to nearly flip onto the orbital plane of the binary, independently of the initial spin-orbit alignment. Moreover, we show that the spin flip due to mass transfer can easily dominate over tidal synchronisation in any giant stars and main-sequence stars with masses $\sim1.5$ to $5\msun$. Finally, the general equations of motion, including tides, are used to evolve a realistic population of massive binary stars leading to the formation of binary black holes. Assuming a strong core-envelope coupling, the resulting tilt of the first-born black hole typically reduces its spin projection onto the orbit normal by a factor $\sim\mathcal{O}(0.1)$. This result supports previous studies in favour of an insignificant contribution to the effective spin projection, $\chi_\text{eff}$, in merging binary black holes.