Recently, I was sent a few fairly similar questions:
The Milky Way will indeed collide with the Andromeda galaxy in 4 billion years, and modeling shows that they will have to merge together (although galaxies may collide and not merge, but scatter after the collision in different directions, greatly deforming).
The fate of the Earth in such a collision depends on how the stars interact and move. Cosmic distances are enormous and a direct collision of stars in a collision of galaxies is almost impossible, it’s like shooting a shotgun at ants in Madagascar while sitting in Moscow.
However, in some cases, with the close passage of another star, the Earth can be torn out of the solar system, attached to the system of this star, or it can simply change its orbit, respectively, many scenarios of what will happen on Earth are possible.
If the stars do go into a direct collision (close enough so as not to scatter in different directions), then they will spin around the common center of mass and either form a stable binary system or merge together after a while.
In the process, the Earth can be absorbed by one of the stars or thrown out of the system, but if it survives, then the conditions on it may well become unsuitable for life: too hot, too much radiation, or too high tectonic activity, or all at once.
It is difficult to say for sure due to too many parameters affecting the outcome. However, in the vast majority of possible scenarios, nothing will happen to Earth.