How can you stop your missile locking onto a friendly target?
First off, very rarely will you shoot at an unidentified aircraft.  Most engagements require a visual ID, or a hostile action, prior to engagement. However, you will also program your aircraft with specific ROE logic that will indicate that certain aircraft are friendly, as identified by IFF transponders, Link 16, etc.

You can still lock up friendly aircraft, and we frequently do to facilitate rendezvous with friendly aircraft and to clear ourselves from traffic, but if your master switch goes to ARM and you have a friendly locked up, there's a good chance you won't be flying again for awhile, if ever.

When we lock up an aircraft, 99% of the time, it does not indicate an intention to shoot.  By using our radar and "locking up" a target we get target aircraft altitude, speed, range, target aspect, heading, $Vc$, etc, all useful things that come in handy for routine events.