Cellular adhesion is a key ingredient to sustain collective functions of microbial aggregates. Here, we investigate the evolutionary origins of adhesion and the emergence of groups of genealogically unrelated cells with a game-theoretical model. The considered adhesiveness trait is costly, continuous and affects both group formation and group-derived benefits. The formalism of adaptive dynamics reveals two evolutionary stable strategies, at each extreme on the axis of adhesiveness. We show that cohesive groups can evolve by small mutational steps, provided the population is already endowed with a minimum adhesiveness level. Assortment between more adhesive types, and in particular differential propensities to leave a fraction of individuals ungrouped, can compensate for the cost of increased adhesiveness. We also discuss the change in the social nature of more adhesive mutations along evolutionary trajectories, and find that altruism arises before directly beneficial behavior, despite being the most challenging form of cooperation.