Preventing them is a little tricky; the stitch is essentially caused by the contraction of your intercostal muscles so trying to ensure a good flow of oxygenated blood to those muscles is the key. I usually get my stitches from increasing the demand on my lungs like running up a hill or increasing my speed without adapting my breathing properly, so that's something to think about.
Once its too late though; I find slowing down a little bit so that you can take deeper breaths helps - you can then 'send' the oxygen down to your intercostal muscles more effectively. Giving the intercostals (around the ribs) a little massage (just give them a squeeze and a prod as you're running along) also helps disperse the sensation as you're effectively promoting fresh blood flow to the muscles and decontracting them. Grabbing and/or squeezing and holding the waist can also help.