Updated:

1. Summon a minecart

/summon minecraft:minecart ~ ~ ~2
/give @s minecraft:minecart

Summon it on a rail and it sits on the track. Bedrock uses /summon minecart ~ ~ ~2.

2. Minecart IDs

TypeID
Minecartminecart
With chestchest_minecart
With hopperhopper_minecart
With furnacefurnace_minecart
With TNTtnt_minecart
With command blockcommand_block_minecart

The same IDs work with summon and give. The command block minecart cannot be crafted.

3. Chest minecart with items (Java)

/summon minecraft:chest_minecart ~ ~ ~2 {Items:[{Slot:0b,id:"minecraft:diamond",count:5},{Slot:1b,id:"minecraft:bread",count:16}]}

Slots go from 0 to 26. On 1.20.4 and older write Count:5b. Great for delivery games.

4. A minecart that runs a command (Java)

/summon minecraft:command_block_minecart ~ ~ ~2 {Command:"say The train has arrived"}

It runs its command each time it passes a powered activator rail, perfect for station announcements or fireworks at the finish.

5. Put riders in

/ride @s mount @e[type=minecraft:minecart,limit=1,sort=nearest]
/summon minecraft:minecart ~ ~ ~2 {Passengers:[{id:"minecraft:villager"}]}

The first line seats you in the nearest minecart (Java 1.19.4+); the second summons one with a villager inside. On Bedrock use /ride @s start_riding @e[type=minecart,c=1]. See the ride command.

6. Lay a powered rail line

/fill ~ ~-1 ~1 ~ ~-1 ~30 minecraft:redstone_block
/fill ~ ~ ~1 ~ ~ ~30 minecraft:powered_rail

Redstone blocks underneath power the rails, giving a 30-block boost lane heading south. See the fill command.

7. Effects for riders only (Java)

execute as @e[type=minecraft:minecart] on passengers if entity @s[type=minecraft:player] run effect give @s minecraft:night_vision 3 0 true

on passengers switches to whoever rides the cart. In a repeating block, riders get night vision for dark tunnel rides.

8. Remove minecarts

/kill @e[type=minecraft:minecart]
/kill @e[type=minecraft:chest_minecart]

Chest minecarts drop their contents, so follow up with /kill @e[type=item] (kill command).

Troubleshooting

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.