How to solve for composition function?

If $$f(x)=x+6, h(x) = 6x-2$$ and $$g\circ f = h$$, how can I solve for $$g(x)$$? I know how to do it on paper, but I wonder if Mathematica can do it in a general way. This doesn't work:

f[x_] := x + 6;
h[x_] := 6 x - 2;
Solve[ (g @* f )[x] == h[x], g]

• Unfortunately I think mathematica can't solve for functions per se...however, you could possibly try to use InverseFunction in some way to express $h \circ f^{-1}$, or do something clever by somehow turning this into a differential equation for g. That would be cool. Of course both of these approaches have restrictions, and I'd be happy to see a more general approach. Jul 3 at 0:32
• Formally: Composition[Function[x, 6 x - 2], InverseFunction[Function[x, x + 6]]][x] Jul 3 at 17:20

Solve basically works on the level of symbolic algebra, so break down the composition in terms of substitution:

f[x_] := x + 6;
h[x_] := 6 x - 2;

g[u_] = g[u] /. First@Solve[{g[u] == h[x], u == f[x]}, g[u], {x}]
(*  2 (-19 + 3 u)  *)

ClearAll[g];
g[f[x]] == h[x] // Simplify
(*  True  *)


Restriction: As @thorimur's comment implies, f must be invertible; further Mathematica has to be able to somehow solve for the inverse. Note in the above Solve command, f does not actually appear itself, just its value, so that solving for the inverse is merely a simple algebra problem. Yet, Mathematica can use InverseFunction:

Clear[f, g]

g[u_] = g[u] /. First@Solve[{g[u] == h[x], u == f[x]}, g[u], {x}]


Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information.