Derailleur hangers used to be waify little things that would twist if you so much as rode through tall grass. Now, in the thru-axle era, they've become structural members of our frames. It's a lot harder to bend them, which means it's a lot harder to bend them back. Hard enough that you might over-do it. So, if you bash your mech on something stiffer than tall grass, and you've still got some functional gears, it might be best to wait until you're in civilization before making an attempt. But if you've got no other choice, there are some best practices you should observe.
First, identify that it's bent. Besides a telltale dusty gouge from bashing a rock, you should notice your shifting is a lot less accurate in the easier gears than the harder ones. That's because the derailleur is reaching farther to get to those bigger cogs, so a small bend makes a bigger difference. The vast majority of bends are pretty straight in, so to get rolling, you just bend it straight out. But again, hangers are pretty tough these days. The best technique is to combine force on the derailleur's mounting bolt as well as the derailleur itself. I like to start by leaning the rear wheel against a tree with my knee against the bottom of the tire. This keeps the bike from lifting under the force you're about to put on the derailleur. Hold a 5-millimeter Allen wrench in the bolt with one hand and the derailleur body with the other. It's crucial not to grab too low on the derailleur, otherwise you might bend the cage, which is even trickier to straighten. Depending on your particular derailleur hanger, it'll take a lot of force to get it to move. Get a feel for how stiff it is, and adjust the force you're putting into it accordingly. Check your work often. Once you've got reasonable results, it's probably best not to push your luck. Get it to a shop with a proper tool, or just replace the hanger altogether.