The article is based on Russian, Georgian and Ossetian sources. It is by no means perfect or exhaustive, it was written in haste and I would welcome any comments, corrections and additions.
The Ossetians came to what is now Georgia after the attacks of Mongols on the Ossetian country in the northern Caucasus. Georgia, also weakened by the Mongols, needed help against the never stopping raids of the Persians and Turks, so they let the Ossetian occupy the lands in exchange for the military assistance. Georgian historians, though, say that the Ossetian invaded weak Georgia and even occupied Gori. In 14th century king George V restored the unity of Georgia, but very soon the invasion of Timur turned Georgia into a mess of small feudal duchies again. The Georgian feudals often used Ossetians in their wars. In exchange they granted their allies parts of land of the neighbor feudals. In the end, Ossetians lived in relatively small groups between Georgians. In some places, though, higher in the mountains, they lived in a more homogeneous environment. Also, in the Alazani valley, in the south of Kakheti (one of Georgian kingdoms), they formed a large enclave.
In 18th century the rulers of Kartli and Kakheti were named Wali (governor) of Gurjistan and the kingdoms belonged to Iran. These governors were obliged to convert to Islam and the only one who refused to do so, was George XII, son of Erekle II, who had to ally with Russia to save Georgia. In 1799, before his death, he wrote to his ambassador: "Give them [Russians] all my kingdom and my possession as a sincere sacrifice, and offer it not only for the protection of the Russian empire, but leave it at their discretion, so since this time the kingdom of Kartli would belong to the Russian state with all the rights that other areas of Russia have".
Georgian feudals Machabeli and Eristavi became the lords of the lands now known as South Ossetia. They competed and South Ossetia belonged sometimes to one of them, then to another, then Erekle II would take them to himself, then grant to one of these families again and so on. Ossetians, like normal peasants, often rebelled.
In attempts to get rid of the feudals, Ossetians in 1749 attempted to ask Russia for protection. Some sources say that in 1774 the united Ossetia (South and North) joined Russia. I have no information about any evidences except for some vague references to the treaty of Kuchuk-Kainarji.
In 18th century, the situation in Georgia was quite complicated. The Western Georgia (kingdom of Imereti) failed to unite. Guria, Svanetia, Abkhazia and Megrelia strived for independence. In an earlier article I wrote briefly:
"In early XVIII century, the Georgian king Vakhtang VI and other Georgian political figures find an asylum in Russia. In the end of 1782, king Erekle II asks Catherine to take Eastern Georgia (kingdom Kartli-Kakheti) under protection and in 1783 the treaty of Georgievsk was signed. Eastern Georgia gave up its autonomy and Russia guaranteed its independence and territorial integrity. Russia promised to increase the number of troops in Georgia, but failed to comply. In 1795, Persia attacked Georgia and sacked Tbilisi. Two Russian battalions retreated, but Russia sent 13,000 soldiers and liberated Georgia. Turkey also gave up all claims. In 1798, Erekle died and his son George XII became the king. Soon, his illness provoked instability in the country, when his son David and his half-brother Yulon contested the throne. Russia backed David and in December 1800 David became the regent of the Eastern Georgia. In January 1801 Russia violated the terms of the treaty of Georgievsk, removed David from power and proclaimed Eastern Georgia a part of the Russian empire. David was brought to St. Petersburg under a military escort, but in Russia he was freed, he settled in St. Petersburg, became a general and an important politician and in 1812 became a senator. In 1810, the Western Georgia (kingdom of Imereti) joined the Eastern Georgia and became a part of Russian empire."
The whole story is a bit longer and more complicated, but it'll do for now. Famous Georgian writer Ilya Chavchavadze wrote: "Since that memorial day Georgia recovered peace. The protection of the great brotherly nation dissipated the eternal fear of the enemies. The tired country calmed down, rested from devastation and havoc, from never ending wars and struggle".
The tsarist government attempted to normalize the relations between Georgian feudals and Ossetian peasants. A group of anti-Russian Georgian nobles led by prince Yulon tried to use Ossetians against Russia. Since the Eristavi family eagerly cooperated with Russia, it was easy to talk Ossetians into rebellion against Eristavi and Russia. In 1801 Ossetians rebelled. In 1802 they killed the hated Machabeli. Russia sent troops to South Ossetia. The commander, Simonovich, attempted to avoid conflicts, contacted the Ossetian elders and soon he convinced them to stop attacking Russians. Ossetians agreed, but demanded that the tyranny of Georgian aristocrats be stopped. Eristavi and Machabeli wanted to get their feuds back, but Russia refused to restore their power and in 1832 they began terror against the Ossetian peasants.
In 1837 a deputation from South Ossetia asked Nicholas I to judge whether Eristavi and Machabeli have the right to be their masters. The special commission in St.Petersburg ruled that Machabeli failed to prove their rights as landowners in South Ossetia. However, the court in Tiflis (the old name of Tbilisi) ruled that the Ossetians failed to prove their right to be free. The question was elevated to the Russian Senate. Nicholas I supported the decision of the St.Petersburg commission, but sweetened the pill granting a huge pension to Machabeli. Machabeli refused to comply. They hoped that the serfdom would be instituted in Ossetia, but these were the last years of serfdom in Russia.
After that the situation was under strict control of the tsarist government till the revolutions began in Russia. In Georgia the power belonged to mensheviks (social-democrats, very similar to bolsheviks, but slightly different). Ossetians backed bolsheviks. In 1918-1920 multiple and bloody conflicts between Georgians and Ossetians took place in Tskhinvali, the capital of Ossetia. In 1920 the bolsheviks proclaimed that the Soviets take the power in one of the regions in Ossetia and asked Moscow for support. Moscow did not reply and soon the uprising was suppressed. Georgians killed 4812 people, burned about 50 villages, sacked the food supplies and forced up to 50,000 Ossetians to excape to Russia. Many of them died of cold, starvation and infections.
After the communist take-over in Georgia in 1921, the communist leaders decided to give a status of the autonomous district to south Ossetia.
During the Stalin's repressions, Ossetians suffered even worse than other peoples. Stalin and Beriya were Georgians, you remember? The Ossetians constituted about 2/3 of the population of South Ossetia, but their percentage in the number of the repressed people was about 90%.
During the war, Ossetians fought together with other peoples of the USSR, but they were special here. 35 Ossetians were awarded the highest military award, the Hero of the Soviet Union, which makes one award for 12,000 people, which is the highest percentage among all peoples of the USSR. There are still very proud of it.
After the war, the strict control of the Soviet state kept the national sentiments hidden. However, Georgia attempted to "Georgify" the Ossetians from time to time. So, in 1939 the Ossetians were obliged to use the Georgian alphabet for Ossetian language. Ossetian place names were replaced with Georgian ones. Moving in the same direction, in 1988 Tbilisi decided that the Georgian language would become the only official state language in all Georgia. In 1989 Tbilisi refused to finance the expenses of South Ossetia and did not sanction the elections to the parliament of South Ossetia. On 10 November 1989 the Congress of the People's Deputies of South Ossetia decided to raise the status of the autonomous district to the autonomous republic withing Georgia. The Congress asked Georgia to approve the decision.
Georgians responded with the move that became the beginning of the open confrontation. On 23 November 1989 a march of Georgian nationalists, up to 40-50 thousand people, who gathered from all Georgia, came to Tskhinvali for a rally. The march was led by the Georgian president, nationalist Zviad Gamsakhurdia, later overthrown by Georgians themselves. The march was stopped near the city by 13 or 15 young unarmed men. The rally did not happen, but the Georgians blocked the city for some days.
On 20 June 1990 the Supreme council of Georgia denounced the Consitution of the Georgian Soviet Socialist Republic and all the laws adopted since 1921. One of such laws proclaimed he founding of the autonomous district of South Ossetia. Thus, the autonomy of South Ossetia was cancelled. Moreover, on 9 December 1990 Georgia adopted the law on the abolition of South Ossetia. On 6 January 1991 Gamsakhurdia sent the Georgian troops to occupy Tskhinvali. After 20 days of fighting Ossetia paramilitary groups forced the Georgians to leave the city, but the war continued.
On 17 March 1991 on the referendum most of the inhabitants of South Ossetia voted for the preservation of the Soviet Union. On 19 January 1992 on another referendum the majority of Ossetians voted for independence and reunification with Russia.
On 29 May 1992 South Ossetia proclaimed independence. On 24 June 1992 Georgia, South Ossetia and Russia signed agreement that led to the end of the war on 14 July 1992. All sides agreed to denounce violence and to form Joint Peacekeeping Forces, run by the Joint Control Commission. By that time Gamsakhurdia was deposed and the new president of Georgia Edward Shevardnadze said: "The Ossetians gave us no pretext for their mass removal from Tbilisi, Gori and other cities. Probably, there were guilty among them too, but we are a large country, a large state and we have to show repentance... I clearly see what a crime has been committed against this people".