This is an alternative that is based on the version without \pgfextra, the rationale being that the pgfmanual v.3.1.4 says about \pgfextra on p. 167
Note that this operation should only be used by real experts and
should only be used deep inside clever macros, not on normal paths.
It is also an attempt to "clean up" the keys in such a way that everything is under the /tikz/grid with coordinates/ directory. It has now the key all help lines which appends some style to both the minor and major help lines. Also the syntax has been attempted to clean up, inspired by this discussion: now you only need to say ticks/.list={top,left} and no longer say true several times.
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter% https://tex.stackexchange.com/a/469213/194703
\def\grd@save@target#1{%
\def\grd@target{#1}}
\def\grd@save@start#1{%
\def\grd@start{#1}}
\def\GridCore{\edef\grd@@target{(\tikzinputsegmentlast)}%
\tikz@scan@one@point\grd@save@target\grd@@target\relax
\edef\grd@@start{(\tikzinputsegmentfirst)}%
\tikz@scan@one@point\grd@save@start\grd@@start\relax
\draw[grid with coordinates/minor help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast);
\draw[grid with coordinates/major help lines] (\tikzinputsegmentfirst) grid (\tikzinputsegmentlast)
(\tikzinputsegmentfirst) rectangle (\tikzinputsegmentlast);
\grd@start
\pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
\pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
\grd@target
\pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
\pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
\pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
\pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
\foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
{\ifticksB
\node[anchor=north] at (\x,\grd@ya) {\pgfmathprintnumber{\x}};
\fi
\ifticksT
\node[anchor=south] at (\x,\grd@yb) {\pgfmathprintnumber{\x}};
\fi
}
\foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
{\ifticksL
\node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber{\y}};
\fi
\ifticksR
\node[anchor=west] at (\grd@xb,\y) {\pgfmathprintnumber{\y}};
\fi}
}
\newif\ifticksL
\newif\ifticksR
\newif\ifticksT
\newif\ifticksB
\tikzset{grid with coordinates/.style={
/utils/exec=\tikzset{grid with coordinates/.cd,ticks=none,#1},
decorate,decoration={show path construction,
lineto code={\GridCore
}}
},
grid with coordinates/.cd,
ticks/.is choice,
ticks/left/.code=\ticksLtrue,
ticks/right/.code=\ticksRtrue,
ticks/top/.code=\ticksTtrue,
ticks/bottom/.code=\ticksBtrue,
ticks/none/.code=\ticksLfalse\ticksRfalse\ticksTfalse\ticksBfalse,
ticks/all/.code=\ticksLtrue\ticksRtrue\ticksTtrue\ticksBtrue,
minor step/.initial=.2,
major step/.initial=1,
major line width/.initial=2pt,
minor help lines/.style={
help lines,
step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step}
},
major help lines/.style={
help lines,
line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
step=\pgfkeysvalueof{/tikz/grid with coordinates/major step}
},
all help lines/.code={\tikzset{grid with coordinates/.cd,
major help lines/.append style={#1},
minor help lines/.append style={#1}
}}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw[grid with coordinates={ticks/.list={top,left},
all help lines={draw=green!60!black},
}] (-2,-2) -- (7,4);
\end{tikzpicture}
\begin{tikzpicture}
\draw[grid with coordinates={ticks=all,
all help lines={draw=blue},minor help lines/.append style={dashed},
}] (-2,-2) -- (7,4);
\end{tikzpicture}
\begin{tikzpicture}
\draw[grid with coordinates={ticks=none,
all help lines={draw=red},minor help lines/.append style={densely dotted},
}] (-2,-2) -- (7,4);
\end{tikzpicture}
\end{document}

color=green,(or any other colo of your choice) tominor help lines/.style=andmajor help lines/.style=to color all lines of the grid. – leandriis Aug 31 '19 at 20:44