Work on Only One Project: I know that we all love side projects or maintaining open source projects but remember that context switching is killing our productivity. If you want to stay with a clear mind you need to stop switching on projects.
Own a Reserved Workspace: Your most important workspace is your computer. Only hold things for your job inside it. Remove games and logout all accounts that are not related to your job. I don't say be hard on yourself, just don't hold those distractions further than one click away.
Working Anywhere is Not for You: You need to label a fixed place as a workplace. Reserve a desk (or a room if you can) in your house and go there when you're working.
Use Mindmaps: It's possible to get lost when dealing with the details of your code. When you're starting a new task first analyze steps that you will follow. Draw a map (or make a todo list) on a paper. Try to stick in your plan to not to get lost.
Record Meetings: For the people with an easily distracted mind. It's difficult to listen to long conversations. They are accused of not listening and caring constantly. While on a meeting, a word or sentence that will drag their mind to other places. That's very bad. But it's good news that we're living in 2020. Every mobile phone has a voice recording feature. Put your phone on the table to listen to the whole conversation later. If you don't think It's ethical you can find another solution. Taking notes as much as possible can be another solution.
Never get a task from phone or verbal conversation: Ask them to send a mail or open issue on your issue tracker.
Send Notes to Your Future Self: When you're leaving your task for a while (for example it's Friday and you will continue on Monday). Create a note for yourself like you're handing over that job to another person. Type down something like: I've done this and this, there was a problem on this, I made a research on this but didn't work. I need to check this and this.
Test Your Code Again Maybe you've changed an unimportant one line code but just test it again before submitting.
Create Rituals You need some rituals to tell your brain that is time to work. For example, cleaning your computer with a wet wipe or drinking a coffee can be your ritual. Also finishing the day must have its ritual. (Taking notes for next day is a good one for me)
Keep Your Energy Stable Don't start working when you're wasted. Also, don't start working when your body too energetic to stay still and work.
Stay Away Music With Lyrics Songs with lyrics are your enemy. You can not talk to yourself when you're listening to someone else's words. Also, these words can drag your mind to different places. Listen to instrumental music or use noise-generating applications like noises.online
Have Treatment If You Need If you're struggling with attention keeping your real problem may be depression. It's a well-known fact that low levels of dopamine cause concentration problems. Consider visiting a psychiatrist.
Love Yourself It's hard to live with attention problem for you, your colleagues and also your family. It's easy to hear negative comments about you. But remember that keeping negative thoughts about yourself is not a solution. Remember that your unconscious mind tries to form your mind by following your thoughts. Keep away from saying "I'm stupid" when things are going bad. Just work on them.