Prior to the Hagana

Before the arrival of the Second Aliya (1882-1903), the old Yishuv Jews (in 1882, Palestine's population was composed of 25,000 Jews and 250,000 Arabs) were considered by the rest of the non Jewish inhabitants to be "proteges" and "children of death" (walad el-maita). Meaning: defenseless people, dependant on the mercy of the aggressors - the Turks, Arabs, or other foreign consuls.
Members of the First Aliya (the first wave of immigration to Israel), who founded Rishon Le'zion, Zichron-Ya'acov, Petah-Tiqwa, Rosh-Pina, Metula and Hadera, strove to change this humiliating situation by establishing the Shomrim (the Jewish Watchmen) lead by Avraham Shapira. At the same time, Jewish, Arab, Bedouin and Circassians Watchmen protected Jewish lives and property, including the new Jewish settlements.
Second Aliya members took the initiative a step further by creating a Jewish work force, called the Bar-Giora Group, whose purpose was to defend its own land and whose motto was: "In Blood and Fire Judea fell down - In Blood and Fire Judea will rise". The organization was kept clandestine for safety reasons.
The organization expanded in 1909, and became Hashomer (the Jewish Guardsman organization). During the ensuing years, brave and well trained organization members defended the Jewish settlement: beginning with the lower Galilee settlements, then moving on to Hadera and surrounding settlements, and later on defense of the southern settlements.
Hashomer existed for 11 years (1909-1920). Many of the Yishuv members acquired military experience while fighting in the British army (Jewish battalions) during WW I, and contributed their military experience to the small Yishuv.
The Jewish Defense force developed rapidly from 1909 up to the end of WW I. Bar-Giora numbered only a few, Hashomer on the other hand numbered many dozens and the Jewish battalions, including Gdud Nahagei Hapredot (the Muleteers) corps in the British Army established in 1915-1916 in Britain and the U.S, and the first Judean regiment, established in Palestine, numbered several thousands of members.
Based on those foundations Hagana (Defense force) was formed in 1920.