Answer by taking a quote from the following article:

Garibaldi was born and christened Joseph-Marie Garibaldi on 4 July 1807 in Nice, which had been directly annexed by First French Empire in 1805, to Ligurian family of Giovanni Domenico Garibaldi from Chiavari and Maria Rosa Nicoletta Raimondo from Loano. In 1814, the Congress of Vienna returned Nice to Victor Emmanuel I of Sardinia; nevertheless, France re-annexed it in 1860 by the Treaty of Turin, which was ardently opposed by Garibaldi. Garibaldi's family's involvement in coastal trade drew him to a life at sea.

Garibaldi returned to Italy amidst the turmoil of the revolutions of 1848 in the Italian states and offered his services to Charles Albert of Sardinia. The monarch displayed some liberal inclinations, but treated Garibaldi with coolness and distrust. Rebuffed by the Piedmontese, he and his followers crossed into Lombardy where they offered assistance to the provisional government of Milan, which had rebelled against the Austrian occupation. In the course of the following unsuccessful First Italian War of Independence, he led his legion to two minor victories at Luino and Morazzone.  After the crushing Piedmontese defeat at Novara (23 March 1849), Garibaldi moved to Rome to support the Republic recently proclaimed in the Papal States, but a French force sent by Louis Napoleon (the future Napoleon III) threatened to topple it. At Mazzini's urging, Garibaldi took command of the defence of Rome. In fighting near Velletri, Achille Cantoni saved his life. After Cantoni's death, during the Battle of Mentana, Garibaldi wrote the novel Cantoni il volontario.  On 30 April 1849 the Republican army, under Garibaldi's command, defeated a numerically far superior French army. Subsequently, French reinforcements arrived, and the siege of Rome began on 1 June. Despite the resistance of the Republican army, the French prevailed on 29 June. On 30 June the Roman Assembly met and debated three options: surrender, continue fighting in the streets, or retreat from Rome to continue resistance from the Apennine mountains. Garibaldi made a speech favoring the third option and then said: Dovunque saremo, cola sara Roma. (Wherever we may be, there will be Rome).  A truce was negotiated on 1 July, and on 2 July Garibaldi withdrew from Rome with 4,000 troops. The French Army entered Rome on 3 July and reestablished the Holy See's temporal power. Garibaldi and his forces, hunted by Austrian, French, Spanish, and Neapolitan troops, fled to the north with the intention to reach Venice, where the Venetians were still resisting the Austrian siege. After an epic march, Garibaldi took momentary refuge in San Marino, with only 250 men still following him. Anita, who was carrying their fifth child, died near Comacchio during the retreat.

How did the war end?
A truce was negotiated on 1 July, and on 2 July Garibaldi withdrew from Rome with 4,000 troops.