According to the first law of thermodynamics
\begin{align}U=TS+YX+\sum_j\mu_jN_j.\end{align}
Where $Y$ is the generalized force, $dX$ is the generalized displacement.
Helmholtz Free Energy
\begin{align}F=U-TS=YX+\sum_j\mu_jN_j. \end{align}
Gibbs Free Energy
\begin{align}G=U-TS-YX=\sum_j\mu_jN_j\end{align}\begin{align}G=U-TS-YX=\sum_j\mu_jN_j.\end{align}
Therefore that
\begin{align}G=F-YX \end{align}\begin{align}G=F-YX.\end{align}
In your case, $Y=H$, $X=M$, so we get
\begin{align}G=F-HM.\end{align}
You can see the textbook:
A Modern Course in Statistical Physics by L. E. Reichl, 2nd, ed (1997), p23, 42, 45.