The problem of sustaining a public resource that everybody is free to overuse—the ‘tragedy of the commons’—emerges in many social dilemmas, such as our inability to sustain the global climate. Public goods experiments, which are used to study this type of problem, usually confirm that the collective benefit will not be produced. Because individuals and countries often participate in several social games simultaneously, the interaction of these games may provide a sophisticated way by which to maintain the public resource. Indirect reciprocity, ‘give and you shall receive’, is built on reputation and can sustain a high level of cooperation, as shown by game theorists. Here we show, through alternating rounds of public goods and indirect reciprocity games, that the need to maintain reputation for indirect reciprocity maintains contributions to the public good at an unexpectedly high level. But if rounds of indirect reciprocation are not expected, then contributions to the public good drop quickly to zero. Alternating the games leads to higher profits for all players. As reputation may be a currency that is valid in many social games, our approach could be used to test social dilemmas for their solubility.