If the Republican Spain was a democratic Government why did they allie with the Comunnists in the Spanish Civil War against the Nationalists. For Example they were with the UGT (communists) and the CNT/FAI (anarchist).
A slightly slow response here as I was away over the weekend, but I did want to chime in. With respect to u/GrizzlyAdamsPetBear’s answer, I think I interpreted the question’s intent a bit differently than they did. To my mind, the crux of the question is why the Republican government fell back on support from the far left (including, but not limited to the Communist Party of Spain) rather than try and move to the political centre and make common cause with the centre-right elements of Spanish politics, in an attempt to prevent both a military coup from the right, or a socialist revolution from the left. In fact, such an attempt was made, but far too late to have any chance of success, with the overture coming even as the military coup was set in motion. Stanley Payne attributes this to complacency, with the Popular Front government believing that they did not face a serious threat from the military until the very last minute. It also didn’t help that the main centre-right party which had previously held power (Radical Republicans) had been more or less wiped out at the February 1936 elections.
There are a few points to clarify about the make-up of the Spanish government on the eve of the civil war, which was far from dominated by communists and anarchists. It’s worth remembering the way that Spanish elections worked under the Republic. Electoral law meant that the winner in a particular district took a disproportionate number of seats compared to their vote share, which in turn meant that running as a coalition was highly advantageous. Essentially, the Popular Front was one such coalition, made up of a broad spectrum of left-wing parties, who agreed on a basic platform, and on how the seats won by the coalition were to be distributed. This distribution agreement favoured the centre-left – the Left Republicans (including their Catalonian offshoot) and the Spanish Socialist Party (to clarify a point from your question – the UGT were affiliated to the socialist PSOE, not the communists). The Communist Party, while important supporters of the Popular Front agreement, ended up with only 15 seats in the new parliament, and only two seats went to anarchists. Anarchists did not boycott the election, as they had done previously, but the main anarchist organisations (chiefly CNT-FAI) still did not run candidates. Essentially, both communists and anarchists were willing to support the Popular Front to prevent a far-right victory, but for their own reasons (and, in the communists’ case, their relative weakness) were willing to accept minimal parliamentary representation for their efforts.
So, speaking in terms of parliament at least, communists and anarchists were entirely marginal to the political make up of the new Popular Front government, with neither party being represented in the new cabinet. Yet the issue of parliamentary support became secondary on the outbreak of the military uprising. For all intents and purposes, central government disintegrated in the aftermath of the coup, with local developments becoming contingent on local loyalties and leadership. Such a situation favoured the left-wing parties, not only because they had a more radical membership base that could be more readily mobilised, but also because of the mass membership of their affiliated trades unions such as the UGT which could also be mobilised. As these groups armed themselves and formed militias, their importance was much more vital in defending the Republic than parliamentary majorities in the early weeks of the war. These changing political/military realities led to a shift leftward in the Popular Front government as it re-established meaningful central control – more socialists were appointed to key cabinet positions formerly held by Left Republicans, including the Prime Minister and defence portfolios, while for the first time small numbers of anarchists and communists were brought into the cabinet.
The increasing reliance of the Republican government of the far left, therefore, reflected the simple realities on the ground, with a variety of left-wing organisations having effectively taken localised power across much of Republican territory and their cooperation was required to restore central government. Maintaining an effective, unified war effort therefore depended on maintaining political unity across this broad spectrum. This was, of course, difficult to do, and the failure to preserve unity has often been cited as the key failing of the Republican side in the civil war – such as in Barcelona in May 1937, which saw a communist-led move to suppress the small Partido Obrero de Unificación Marxista (POUM), as famously described in Orwell’s Homage to Catalonia. The communists themselves were perhaps the main beneficiaries of wartime developments, and the war saw a substantial increase in their influence in both politics and the military – I’ve discussed this in a past answer. I’d also highlight, as mentioned in that answer, that it would be a mistake to assume that communists and anarchists represented non-democratic forces, as seems to be indicated in your question. Both groupings regarded themselves as more, rather than less, democratic that contemporary liberal parliamentarians. While with hindsight we can see that Stalinist communism in particular was in no way democratic, that doesn’t mean that contemporary adherents in the 1930s were insincere in their beliefs, or that their participation in a coalition to defend democracy in Spain was entirely cynical.
You know that phrase "the enemy of my enemy is my friend"?
One point of concern here is that, if you are an American, you associate the word "Republican" with conservative ideals of small government, free markets, and social conservatism, while "Liberals" stand for big government, regulated markets, and socially progressive beliefs. Those definitions don't carry water outside of the specific context of post-war America. "Republicans" in the Spanish context refers to supporters of a democratic Spain, with a government formed through the will of the people. "Liberalism" is a complicated ideology, but it could be summarized as a belief in free speech, free religion, and free markets.
The Republic of Spain came into existence in 1931, after King Alfonso was deposed and a democratic government was established. The initial government was formed by several reformist parties encompassing liberals and socialists. They pushed through a number of reforms that seem uncontroversial to our modern ears - universal suffrage (including women's suffrage), land reforms to eliminate the remnants of serfdom, and restrictions to the role the Catholic church played in civic life.
But they were very controversial! So much so that a conservative government, formed by groups ranging from Monarchist to Fascist, won the next major election. This coalition, called the Spanish Confederation of the Autonomous Right, advocated for the return to an authoritarian, Catholic form of government, and aimed to put a halt to those liberal reforms. They were met with strikes and direct actions from socialist, communist, and anarchist groups that managed to significantly weaken the conservative government and push the liberal parties leftward.
In the 1936 election, a coalition of left wing parties called The Popular Front won enough votes to form a government. The popular front included a number of different parties with differing ideologies, from the liberal Republican Union to radical Anarchist groups. Though these groups disagreed strongly about many issues - particularly whether Spain would be a capitalist or communist country, not a small disagreement - they were able to align with one another over their opposition to the conservatives and fascists.
To be clear, this is the crux of your question. In 1936, the legitimate government of the Spanish Republic was largely made up of communists, anarchists, and other socialists (as well as a large group of liberals), as chosen by the electorate. To defend the Republic meant to defend a left-wing government.
That government was challenged by the conservative coalition. Political violence was not particularly uncommon in this time, but right-wing groups and left-wing groups clashed viciously in the months after the election. The conservatives sought the aid of the Spanish army in order to prevent a "communist takeover" of Spain. Large chunks of the military - especially in Spanish-controlled Morocco - answered this call, and six months after the 1936 election, declared their intention to defeat and replace the Spanish Republic's government. One of the leaders of this military coup was Francisco Franco, who had previously made a name for himself putting down those strikes mentioned above. And the rest is brutal, destructive history.
In summary, "Republicans" didn't really ally with "the Communists" - at the outbreak of the war, they were one and the same. There were other Communist and Anarchist groups who opposed the Spanish Republic because, to them, it represented a bourgeoisie capitalist state. But when Franco's planes flew overhead, many of them took up arms to defend it all the same.
ETA: crrpit's answer adds a lot of nuance to this general overview. Heartily endorsed!