How to set a contribution to another membership for the same member
You can use the Membership Payment extension for this.

This extension adds a new Membership field when editing a contribution.  You can use this to add, remove and move a contribution's association with a membership of that same contact.

See screenshot below: