I self-harmed for 9 or so years. I've had one slip-up in the last 3 years, but otherwise I have lost the desire to cut. The things that helped me were: learning to feel my feelings nonjudgmentally and mindfully (takes a lot of practice), working on self-compassion, and having alternate coping behaviors (especially body-centered ones like walking or stretching). During times of high emotion, I have urges to cut - I observe the urges with kindness, and know that I don't need to act on them. There's usually a feeling of urgency, so that makes it more difficult, but knowing that the emotion will pass eventually makes it easier to tolerate in the moment. I think DBT therapy is very helpful for self-harm. Ongoing mindfulness practice has been the game-changer for me personally.
After the slip-up, I had to remind myself that I hadn't thrown away all my progress, and it didn't mean that I needed to cut again. That helped me let go of trying to be perfect, and helped me build resilience.