An exercise on category theory and product 
This is from Lecture Notes in Algebraic Topology, 2nd by Davis and Kirk.
My try of the direct proof involves a step $$h\circ g\circ f=h\circ f\implies g=\mathrm{id}$$ for which a counterexample can be found in the category $\mathbf{Set}$.
How should I bypass this?
 A: I don't know how you got to the equality you write; unless you have some compositions written on the wrong side by mistake... Since you don't show the derivation, I can't really comment on whether one can fix your approach, or if there is some simple error in it.
Anyway...
This is a typical situation. Consider what the universal property of the product gives you about both $(X,\pi_1,\pi_2)$ and $(X',\pi_1',\pi_2')$:


*

*We have morphisms $\pi_i\colon X\to X_i$.

*Given any object $Y$ and morphisms $f_i\colon Y\to X_i$, there exists a unique morphism $\mathfrak{f}\colon Y\to X$ such that $f_i=\pi_i\circ \mathfrak{f}$.

*We have morphism $\pi_i'\colon X'\to X_i$.

*Given any object $Z$ and morphism $g_i\colon Z\to X_i$, there exists a unique morphism $\mathfrak{g}\colon Z\to X'$ such that $g_i = \pi_i'\circ \mathfrak{g}$.


Now, by 3 and 2, there exists a (unique) morphism $h\colon X'\to X$ such that $\pi_i' = \pi_i\circ h$.
And by 1 and 4, there exists a (unique) morphism $k\colon X\to X'$ such that $\pi_i = \pi_i'\circ k$.
Now, consider $k\circ h\colon X'\to X'$. Note that $\pi_i'\circ (k\circ h)=(\pi_i'\circ k)\circ h = \pi_i\circ h = \pi_i'$. However, using 3 and 4, we know that there is a unique morphism $j\colon X'\to X'$ such that $\pi_i' = \pi_i'\circ j$. Now, clearly $\mathrm{id}_{X'}$ works; but then so does $k\circ h$ by our computations. So the uniqueness clause of the universal property implies that we must have $k\circ h = \mathrm{id}_{X'}$.  A similar argument should be used to show that $h\circ k = \mathrm{id}_{X}$.
