If you experience pain while doing yoga then either change what you are doing, or change the way that you do it.
I injured a knee in a motorcycle accident almost 18 years ago. At the time I was practicing Ashtanga yoga. My knee injury (probably a torn ACL) prevented me from doing lotus pose and a lot of other closed knee postures. So I stopped doing them.
I could do pigeon, but I had to move into it and out of it very carefully.
So when dealing with pain while doing yoga, the first thing you can do is not do the pose that causes the pain. The other thing you can do, is move in and out of any position slowly, smoothly, carefully. The slow and smooth is important because it means you aren't doing any sudden movements. It also means you have to focus on what you are doing. And it also means that you can stop as soon as you feel any precursors to pain.
Another way to deal with pain is to learn your body. Often times, if the body isn't working properly, some part of it will lack stability. And so your brain will tighten up some part of the body in order to create stability. Over time, this can lead to pain from over use.
One way around this is to consciously create stability where it is needed.
And this is where understanding comes in. The better you know your body the easier it is to figure out where you should try to create stability. And how you should try to create it.