You should replace your chain every year or 1000 miles. If you ride hard, or ride in mud and dust, you may need to replace it more frequently. If you've broken your chain, there may be some warp you can't see, and the chain will eat away at the teeth of your rings. Plan to replace a repaired chain as soon as possible.
You probably need to cut the new chain down to size. It should contain the same number of links as your original chain. If you use the chain straight out of the box, it will usually be 3 or 4 inches too long, and the idler pulley may not hold the chain tight enough while you ride.
Put the new chain alongside your old one on the floor. You may notice that the links at the far end don't line up exactly -- because your old chain is worn and has stretched. Match the chains so your new chain has as many links as the old one. Before "breaking the chain" to the length you need, consider how the ends must fit together. If you're using a Quick Link, you need two inner links facing each other, because the Quick Link replaces the outer link that would normally join them. If you're using a pin, you need an outer link on one end, and an inner link facing it. (You DON'T want two inner links facing other, nor two outer links -- you want an outer link on one end, and an inner link on the other, OK?).
If you're starting from scratch, you'll need to put the chain on the bike to estimate the correct size. In general, the chain is the correct length if (1) the chain runs vertical between the upper pulley and lower pulley of the rear derailleur when shifted into the small chainring and the small cog on the rear, and (2) the chain still has "room to bend" when the derailleur comes forward when in the large chainring and largest rear cog.
(On some bikes with very large rear cogs, it may not be possible to shift into the lowest rear gear -- big cog greater than 32 teeth -- while in the highest front gear -- big chainring. In this case, size the chain to the small chainring - small cog combination as in #1. If the chain is too long, it may skip gears.)
Using a chain-breaking tool, push the pin out of the bushing. If you'll be using a Quick Link connector or a breakaway pin, you can push the pin all the way out.
If you plan to use the existing pin to join the chain, you'll need to stop while the pin is still within the far-side outer side plate. Check the position of the pin frequently. Stop turning when the pin is just on the edge of the bushing, before it begins to move through the outer side plate. (This is about 6 complete turns of the chain tool.) Now back the chain tool out of the bushing.
Before joining the chain, double-check the routing. The most common mistake is to assemble the chain so it's dragging over the metal support strut between the top pulley and the idler pulley of the rear derailleur.
Align the chain end with the inner side plate link so it fits inside the end containing the pin and outer side plate link. Using the chain tool, push the pin inward until it's evenly spaced within the outer side plates.
Some chains come with a special connecting link. This link joins two inner plates together. If your chain came with a connecting PIN rather than a quick-link, you can buy these links separately for chain repair. You need to buy the right size (7-speed and 8-speed bikes use one size chain; 9-speed rear cassettes use a thinner chain).
Slide the pins into place through the larger side of the opening in the opposite plate (see photo). Squeeze the link until the pin ends are protruding from each side. Now pull hard until the link lengthens and the pins lock into place.
Some chains come with special connecting pins. These have a tapered guide to help you insert the pin
If you're buying these pins as spares, be sure to get the correct size. If you have a 7-or 8-speed rear cassette, your chain is wider, and you need a slightly wider pin than one for a 9-speed chain.
Attach your chain tool so it's pushing on the protruding pin. Be careful not to put sideways stress on the pin -- you don't want to pop the pin off the guide now!
Position the pin so it appears evenly spaced inside the link.
If necessary, fine-tune the position of the pin within the chain, so it doesn't protrude from either side.
Your new chain will be a bit stiffer side-to-side. You may find that your shifting has changed a bit. If so, fine-tune your front or rear derailleurs .