To prevent excessive gas, it may help to: Avoid the foods that affect you most. Common offenders include beans, peas, lentils, cabbage, onions, broccoli, cauliflower, whole-wheat bread, mushrooms, and beer and other carbonated drinks. If dairy products are a problem, try low-lactose or lactose-free varieties.