If you're having a huge issue with this, I suggest trying to find ways to hold less context at one time.
Write stuff down & get it out of your head more often. Write down all of your ideas & break down your tasks into smaller tasks. Don't try & solve a giant problem all at once without writing down what you're going to do. You may also discover issues earlier.
It doesn't get rid of the issue 100% but it helps a lot. I say this as a dev who has to do a lot of context switching.