Let $G$ be a connected reductive affine algebraic group defined over the complex numbers, and $K\,\subset\, G$ be a maximal compact subgroup. Let $X$, $Y$ be irreducible smooth complex projective varieties and $f\colon X\to Y$ an algebraic morphism, such that $\pi_1(Y)$ is virtually nilpotent and the homomorphism $f_*\colon \pi_1(X)\to\pi_1(Y)$ is surjective. Define \begin{align*} {\mathcal R }^f\big(\pi_1(X), G\big)&= \{\rho \in \operatorname{Hom}\big(\pi_1(X), G\big) \mid A\circ\rho \ \text{ factors through }~ f_*\}\,,\\[6pt] {\mathcal R }^f\big(\pi_1(X), K\big)&= \{\rho \in \operatorname{Hom}\big(\pi_1(X), K\big) \mid A\circ\rho \ \text{ factors through }~ f_*\}\,, \end{align*} where $A\colon G\to \operatorname{GL}(\operatorname{Lie}(G))$ is the adjoint action. We prove that the geometric invariant theoretic quotient ${\mathcal R }^f(\pi_1(X, x_0),\, G)/\!\!/G$ admits a deformation retraction to ${\mathcal R }^f(\pi_1(X, x_0),\, K)/K$. We also show that the space of conjugacy classes of $n$ almost commuting elements in $G$ admits a deformation retraction to the space of conjugacy classes of $n$ almost commuting elements in $K$.