Let $G$ be a finite group with the property that, for every subgroup $H$, there exists a homomorphism $f: G\to H$ such that $f(h)=h$ for all $h\in H$. What possible groups can $G$ be?
If $P$ is a Sylow-$p$ subgroup of $G$, then the homomorphism $f: G\to P$ has a kernel $K$. which is complementary to $P$ (the intersection is trivial since $f$ restricts to the identity on $P$, and $\langle P, K\rangle=G$, since $f(g)\in H$ for all $g\in G$, so $g^{-1}f(g)\in K$. Note that $K$ is normal. Now applying the assumption to $K$, we get a complement to $K$ with is normal itself and a Sylow-$p$, so the Sylow-$p$ is unique and normal, and so $G$ is the direct product of its Sylow-$p$ subgroups.
So now we just need to characterize the Sylow-$p$'s that satisfy this property, so now assume $G$ is a $p$-group. Let $H=Z(G)$. Then the deformation retraction homomorphism gives a normal complement to the center. But in a $p$-group, every nontrivial normal subgroup intersects the center nontrivially, so the complement to the center is trivial, so $G$ is abelian.
Now, if $G$ has a cyclic subgroup $C$ of order $p^2$, then let $H$ be the order $p$ subgroup of this cyclic subgroup. Then a generator $x$ of $C$ can't map to the identity (otherwise all of $H$ would map to the identity), so it must map to a generator $y$ of $H$. But then $x^p$ would have to map to $y^p=1$, which is a contradiction, since $x^p\in H-\{1\}$ (it's in $H$ since it's the $p$th power of something in $C$, and it's not the identity, since $x\notin H$). Thus, $G$ cannot have a cyclic subgroup of order $p^2$, and thus must be elementary abelian. If so, then $G$ does in fact have this property, since it's just a vector space over $\mathbb F_p$, and you can just project onto any subspace.
Therefore, the only $G$ with the deformation detraction property are those that are the direct products of prime cyclic groups. However, I was wondering if there were an elementary way to do this, since apparently, this problem was from before Sylow subgroups or the Fundamental Theorem of Finite Abelian Groups were covered.
For any subgroup $K$ let $f_K\colon G\to K$ be a homomorphism that restricts to the identity on $K$, and let $i_K\colon K\to G$ be the inclusion. Then we have a short exact sequence $$0\longrightarrow \ker f_K \overset{i_{\ker f_K}}{\longrightarrow} G\overset{f_K}{\longrightarrow} K\longrightarrow 0.$$ This sequence splits by using $f_{\ker K}$ and $i_K$. Therefore $G=K\times \ker f_K$ for all subgroups $K$. Specifically, every subgroup is a direct factor, from which it follows that every irreducible factor is a cyclic group of prime order and $G$ is a direct product of such groups.
Note the only part that requires finiteness is that $G$ can be written as a product of irreducibles. Finiteness can then be replaced by chain conditions by Krull-Schmidt.