How cooperation and altruism among non-relatives can persist in the face of cheating remains a key puzzle in evolutionary biology. Although mechanisms such as direct and indirect reciprocity and limited movement have been put forward to explain such cooperation, they cannot explain cooperation among unfamiliar, highly mobile individuals. Here we show that cooperation may be evolutionarily stable if decisions taken to cooperate and to change group membership are both dependent on anonymous social experience (generalized reciprocity). We find that a win-stay, lose-shift rule (where shifting is either moving away from the group or changing tactics within the group after receiving defection) evolves in evolutionary simulations when group leaving is moderately costly (i.e. the current payoff to being alone is low, but still higher than that in a mutually defecting group, and new groups are rarely encountered). This leads to the establishment of widespread cooperation in the population. If the costs of group leaving are reduced, a similar group-leaving rule evolves in association with cooperation in pairs and exploitation of larger anonymous groups. We emphasize that mechanisms of assortment within populations are often behavioural decisions and should not be considered independently of the evolution of cooperation.