Minecraft wiki entry for Anvil, associated text: As with the grindstone, a player may repair items by combining two similar items. With the anvil, however, the target retains its enchantments and may gain new ones from the sacrificed items.
Alternatively, a player can use materials originally required in the crafting of the item (iron ingots for iron items with durability, diamonds for diamond items with durability) to repair a single item. One material can repair 25% of the target's maximum durability. This is a very good deal in the case of a chestplate, for example; a full repair (four materials) would total only half of the item's original cost (eight materials). In the case of tools and weapons, however, this may be a significantly less economical option; combining two diamond shovels would cost two diamonds in total, while up to four diamonds could be required to directly repair one. Still, it may be worth making the more expensive upgrade if the enchantments are considered difficult to obtain.