To answer questions like this one really needs to advance into the world of not necessarily connected spaces, such as a finite set of points $C$ on a sphere $X$. Then if $f: C \to Y$ is a map to a space $Y$ one can consider the space $Z= Y \cup _f X$ obtained by identifying some of the points $C$ of $X$ to points of $Y$, which we assume connected, although that can be dealt with.
The idea is to model the geometry by algebra, and this can be done by the algebra of groupoids, and the topogical construction of the fundamental groupoid $\pi_1(X,C)$ on a set $C chosen according to the geometry.
Suppose now that $Y$ is a discrete space $D$, so that $f: C\to D$. Let $G=\pi_1(X,C)$. Then we can construct a new groupoid written $U_f(G) $ or $f_*(G)$ with object set $D$, and morphism $f':G \to U_f(G) $, and having the property that the following diagram of groupoids is a pushout:
$$\begin{matrix} C & \xrightarrow{f} & D\\
\downarrow & & \downarrow\\
G &\xrightarrow{f'} & U_f(G)
\end{matrix}$$
where the vertical arrows are just inclusions as sets of identities.
This construction, which includes those of free products of groups, and of free groups, and indeed of free groupoids on a directed graph, is due to Higgins; it is given in his book Categories and Groupoids and also in Topology and Groupoids.
It is also useful to develop a number of properties of this construction: for example if $G$ is connected and has trivial vertex groups, then $U_f(G)$ is a free groupoid, and so its vertex groups are free groups.
You also have to generalise the standard van Kampen Theorem from the case of a single base point to the case of a set of base points, as was published in 1967, and so gave a theorem which could compute the fundamental group of the circle, a rather basic example in topology. This general theorem is given in the above two books.
For further discussion see this mathoverflow question.