Carbon materials are promising anode materials for rechargeable lithium and sodium-ion batteries, due to their low cost, high capacity, and structural designability. In this work, we selected a waste biomass, garlic stem, as the carbon precursor, and we systematically investigated the effect of pyrolysis temperature and time on their battery performance. We find that 800 °C and 2 h are the best pyrolysis conditions, which leads to the optimal carbon material (800C-2H) with a large layer spacing, abundant defect sites, high surface area, and sufficient micro/meso-porous structures. Due to these favorable properties, this carbon anode exhibits an impressive performance for Lithium-ion batteries, with a very high capacity of 480 mAh g-1 and no significant capacity fading after 3000 cycles. Besides, this anode also shows promising performance for Sodium-ion batteries, where a good capacity of 151.9 mAh g-1 and reasonable cycling of 100 cycles are achievable. We also carried out in-situ X-ray diffraction and in-situ Raman spectroscopy experiments to understand the relationship between carbon microstructures and their Li-ion storage.