Every magnet has a north and a south pole. If you hold two bar magnets close to each other, they will either snap together or push apart, depending on the alignment of the poles. Like poles repel and unlike poles attract, and although the poles on a magnet may seem fixed, they can change under certain circumstances. Even the magnetic poles of planet Earth reverse every million years or so. It's possible to change the polarity of both electromagnets and permanent magnets using simple equipment and techniques.
Reversing the Polarity of an Electromagnet
Flip the switch to turn off the power to an electromagnet, which is a coil of wires through which a current flows. The flow of electricity through the wires induces a magnetic field in the coil -- one pole is at the top of the coil and the pole is at the bottom. Note the polarity of the coil before you turn off the power.
Find the two wires that are attached to the electromagnet and disconnect them, using pliers or a screwdriver.
Reverse the positions of the wires. When you turn on the power, the poles of the electromagnet will be reversed.
Reversing the Polarity of a Bar Magnet
Never leave a solenoid or cylinder of wire unattended when it is connected to a power supply. The coil may get very hot and could become a fire hazard.
Mark the poles of the bar magnets with chalk, if they are not already marked, so that you can identify them. To do this, hold two magnets close together and chalk the letter “A” on the ends of the magnets that are attracted to each other and “R” on the ends that repel each other.
Use pliers to wind copper wire tightly around a cardboard tube to create a coil exactly the same length as one of the bar magnets.. Leave enough free wire at each end of the coil to connect to a battery. Remove the cardboard tube to leave a cylindrical coil of wire. This is known as a solenoid.
Place one of the bar magnets inside the coil. Place the coil on a heat-proof surface, such as a stone slab and attach the wires to the terminals of a battery. An electrical current will flow through the coil, generating a magnetic field by induction. This magnetic field can influence the alignment of particles inside the bar magnet. Only allow a short pulse of energy to flow through the coil before disconnecting it from the battery, or the resulting heat may melt your coil.
Hold the second bar magnet close to the poles of the magnet in the coil. If the poles have switched, the opposite ends of the magnet in the coil will now attract and repel. If this does not happen, take the first magnet out of the coil, rotate it through 180 degrees and replace it.
Reconnect the coil to the battery, being sure to connect each wire to the same terminal to which it was previously connected. Allow a pulse of energy to flow through it, then disconnect the battery. Test the magnet in the coil with the second bar magnet again. You should now find that the poles of the magnet in the coil have switched position.
TL;DR (Too Long; Didn't Read)
Depending on the original strength of the magnet, it may take a number of pulses for its poles to be permanently reversed. Be prepared to experiment with the amount and duration of the pulses, until you achieve a satisfactory result.