<<xAct`xTensor`

$DefInfoQ=False;

DefManifold[M,4,{a,b,c,d,e,i,j,k,l,m,n,o,p,q,μ,ν,ρ,σ,α,β}]

DefMetric[-1, g[-a,-b], CD]

$Pre=Validate;

$PrePrint=ScreenDollarIndices;

DefTensor[phi[],M,PrintAs->"ϕ"]

DefTensor[pi[],M,PrintAs->"π"]

DefTensor[gam[],M,PrintAs->"γ"]

DefTensor[X[],M]

DefScalarFunction[B]

expr=gam[]^2*CD[-a][CD[a][pi[]]]*CD[-α][CD[α][pi[]]]

rule0=MakeRule[{gam[],(1/(Sqrt[1+CD[-a]@pi[]CD[a]@pi[]]))},MetricOn->{a},ContractMetrics->True]

expr1=expr/.rule0

rule1=MakeRule[{CD[-a]@pi[],Sqrt[B[phi[]]]CD[-a]@phi[]},MetricOn->All,ContractMetrics->True]

expr2=expr1/.rule1

expr3=ContractMetric[%, g,AllowUpperDerivatives->True]//ToCanonical

rule2=MakeRule[{CD[-a]@phi[]CD[a]@phi[],-2X[]},MetricOn->{a},ContractMetrics->True]

expr4=expr3/.rule2