Why did Einstein introduce expansion in his equation and later on it was patched?

I read that in developing GR, Einstein's original equation indicated that the universe is expanding and later he quickly dismissed it by adding the cosmological constant. I kept wondering, why did he introduce expansion in the first place? I can understand the importance of cosmological constant when he thought that universe must be static and Hubble's shocking discovery. Again is it a math thing like how Dirac discovered antimatter in his equation?

Clarification: I'd like to know why Einstein formulated his initial equation to allow the universe to expand. Was it intentional or a glitch left unnoticed in the math while he was working on GR? All I know is that regardless whether the universe is static, expanding or collapsing gravity is still an attractive force which doesn't affect the math, right?

• Einstein developed a theory in which the universe turned out to be expanding (he did not introduced an expansion by force). Since at the time the universe was belived to be static he added the cosmological constant in his equations in order to remove the predicted expansion. – Charlie Feb 18 at 5:56

As far as originally adding the term,

It was a prejudice of the time that the universe was constant and eternal, forever unchanging — at least on the largest levels. This led Einstein to add a term to his initial equations in 1917. While the original formulation of general relativity included only the attractive form of gravity, this new term, called the cosmological constant, was a repulsive term. The attractive and repulsive forms of gravity could be tuned to balance one another, resulting in a stationary and unchanging cosmos.

When Hubble published his paper in 1929 showing the universe to be expanding, Einstein reassessed:

With the realization that his earlier prejudice for an unchanging cosmos was wrong, Einstein removed the cosmological constant from his equations. He was reported by physicist George Gamow as having called it his "biggest blunder."

Excerpts taken from Don Lincoln's Op-Ed.

On your clarification, allowing expansion in the original field equations was not his intention. Einstein was chasing after general covariance, and his underlying assumption of a static universe was apart from him arriving at this invariance of physical law. It was only when Friedmann and Lemaître found expanding solutions to his field equations that he modified it,

$$R_{\mu\nu}-\frac{1}{2}Rg_{\mu\nu}=T_{\mu\nu}\hspace{3mm}\Rightarrow\hspace{3mm}R_{\mu\nu}-\frac{1}{2}Rg_{\mu\nu}+\Lambda g_{\mu\nu}=T_{\mu\nu},$$

sticking in the cosmological constant in order to permit steady-state solutions (the only ones he thought to be physical). However, the solutions to this modification were unstable, and a handful of years later Hubble came along to shed some light on the matter.

• And after the Einstein's death, when the acceleration of the expansion was discovered, the issue was reassesed again and it was decided that this term shouldn't be skipped after all. In the end, the value of the coefficient in fron of it needs to be decided experimentaly and we shouldn't just assume it's $0$. – Adam Latosiński Feb 18 at 6:21
• Hi I made some edit for more clarification. – user6760 Feb 18 at 7:24