Demeaning variables in OLS I am analysing some data in R where I have information on $y$ and $x$.
When I run $y = \alpha + \beta\cdot x$ I get the same coefficient on $x$ (i.e. $\beta$) as when I run
$$y = \beta  \cdot  (x - \bar{x})$$
But when I run
$$y - \bar{y} = \gamma \cdot x$$ I get a different coefficient on $x$.
I am really confused. Isn't the coefficient on $x$ meant to capture the covariance between $x$ and $y$, relative to the variance of $x$?
Why is it that demeaning the outcome variable ($y$) changes the coefficient on $x$ (when there is no intercept), but demeaning the independent variable ($x$) doesn't change it?
I tried to figure it out numerically but cannot recover this paradox.

I will discuss a general case of which your demeaning is a special case where $X_2$ is just the constant term, see Utility of the Frisch-Waugh theorem.
The Frisch Waugh Lovell theorem tells us that the coefficient $\hat\beta_1$ on the variables $X_1$ of a regression of $y$ on $X_1$ and $X_2$ can
(a) be obtained as usual by regressing $y$ on both $X_1$ and $X_2$ or
(b) by first regressing both, in separate regressions, $y$ and $X_1$ on $X_2$, saving the residuals of these regressions. These can, using the "residual maker matrix" $M_{X_2}=I-X_2(X_2'X_2)^{-1}X_2'$, be written as $M_{X_2}y$ and $M_{X_2}X_1$. Then,  regress these residuals of the regression of $y$, $M_{X_2}y$, on those for the regressions for $X_1$, $M_{X_2}X_1$, or
(c) by only regressing $X_1$ on $X_2$, saving the residuals of these regressions and regress $y$ on the residuals for the regressions for $X_1$
Now, (b) can be written as, using the standard OLS formula,
$$
\hat{\beta}_{\text{ols},1}=(X_1'M_{X_2}'M_{X_2}X_1)^{-1}X_1'M_{X_2}'M_{X_2}y
$$
(to see this, just apply the general OLS expression $(X'X)^{-1}X'y$ to $X=M_{X_2}X_1$ and $y=M_{X_2}y$).
I skip that this is the same as (a), cf. e.g. Frisch-Waugh-Lovell theorem.
Why is the same as (c)?
Notice that $M_{X_2}$ is symmetric and idempotent (i.e., $M_{X_2}'M_{X_2}=M_{X_2}M_{X_2}=M_{X_2}$), so that we may as well write
$$
\hat{\beta}_{\text{ols},1}=(X_1'M_{X_2}'M_{X_2}X_1)^{-1}X_1'M_{X_2}'y
$$
(or $(X_1'M_{X_2}X_1)^{-1}X_1'M_{X_2}y$, if we wish), which is (c). This is what your regression of $y$ on demeaned $x$ amounts to.
It is (unless $M_{X_2}X_1=X_1\Leftrightarrow X_1'X_2=0$, i.e. unless regressors are orthogonal - in your case, unless $x$ has sample mean zero) however not the same as writing
$$
(X_1'X_1)^{-1}X_1'M_{X_2}y.
$$
This is what your regression of demeaned $y$ on $x$ amounts to.
