It is useful to consider this construction in more generality, as is well written up in the case of groupoids (see Higgins' book "Categories and groupoids"). Let $C$ be a small category, and $f: Ob(C) \to Y$ a function. Then there is a category $f_*(C)$ with object set $Y$ and satisfying a universal property with regard to functors on $C$ whose object function factors through $f$. If $C,D$ are two categories with base points, then you get the free product by taking the disjoint union of $C$ and $D$ and then identifying the base points.

To put this in a more general light, the objects of a (small) category can be regarded as a functor Ob from the category $\mathsf{Cat}$ of small categories and functors to the category $\mathsf{Set}$ of sets and functions, and then this functor Ob is both a fibration (this is about pullbacks) and cofibration (this is about pushouts). There is a basic account of these ideas, which originated with Grothendieck, in

R. Brown and R. Sivera, `Algebraic colimit calculations in
homotopy theory using fibred and cofibred categories', Theory and
Applications of Categories, 22 (2009) 222-251.