Switching between battery and external power sources with reverse polarity protection
In this schematic D1 together with R1 ensures that voltage on pin U1.3 is 0V so that U1 would keep Q1 open until there is a voltage source on External Power. Q1 is there to disconnect batteries from load.