Desertion was fairly common in periods of civil conflict, and it was a perennial problem for Caesar and basically everybody else. For the complications that desertion between rival Roman armies could cause, if you'll bear with me, read the following long extract from Caesar's commentary on the Civil Wars. The context is one of Caesar's personal campaigns against the Pompeian generals Afranius and Petreius, near the Spanish city of Ilerda, in 49 BC. These events occurred only months after crossing of the Rubicon:
On the next day the enemy's officers, dismayed at having lost all prospect of supplies and of reaching the Ebro, took counsel on their other measures. There was one route in case they wished to return to Ilerda, another if they made for Tarraco. While deliberating thereon, word is brought them that their water-carriers are being harassed by our cavalry. Having ascertained this, they distribute numerous outposts of horsemen and auxiliary cohorts, and between them place cohorts of the legions, and set about making a line of rampart from the camp to the water, so that they might be able to get water within their defences, both without alarm and without outposts. Petreius and Afranius share this task between them, and themselves proceed to some distance for the purpose of carrying out the work.
At their departure the soldiers, getting a free opportunity for conversation, come out everywhere, and each one inquires after any acquaintance or fellow-townsman that he had in Caesar's camp and summons him forth. First they all express gratitude to the others collectively for having spared them the day before, when they were in a state of panic: "To your kindness," they said, "we owe our life." Then they inquire about the good faith of the general, whether they would be justified in committing themselves to him, and express regret that they did not do so at first, and that they engaged in a conflict with friends and kinsmen. Stirred by such speeches, the men demand a solemn promise from the general for the life of Petreius and Afranius, fearing lest they should seem to have conceived some crime in their hearts or to have betrayed their party. If these conditions are assured they guarantee to transfer their colours at once and send centurions of the first rank to Caesar as deputies to treat of peace. Meanwhile some bring their friends into the camp to entertain them, others are led off by their acquaintances, so that the two camps seemed already fused into one, and many military tribunes and centurions come to Caesar and commend themselves to him. The same thing is done by the Spanish chieftains whom the enemy had called out and were keeping with them in camp as hostages. These sought for their own acquaintances and guest-friends by whom they might severally have an opportunity of being commended to the notice of Caesar. The youthful son of Afranius also pleaded with Caesar through the envoy Sulpicius for his own and his father's safety. The whole place was full of rejoicing and congratulation, on the one side of those who were deemed to have avoided such perils, on the other of those who were seen to have wrought such achievements without bloodshed; and Caesar in the general estimation reaped a great advantage from his traditional leniency, and his policy met with the approval of all.
When these events were announced Afranius abandons the work that he had begun and returns to camp, apparently resolved to bear with a quiet and equal mind whatever chance should befall. But Petreius does not fail himself. He arms his retinue; with this and his official staff of light-armed men and with a few barbarian horsemen, his own retainers, whom he had been wont to maintain to guard his person, he makes a sudden onset on the rampart, interrupts the soldiers' colloquies, drives our men from the camp, and slays all he catches. The rest gather together and, terrified by the sudden peril, wrap their left hands in their cloaks, draw their swords, and thus defend themselves from the light infantry and horsemen, trusting in the proximity of their camp, and retire to it, defended by the cohorts which are on guard at the gates. When this action was over Petreius goes the round of the maniples and calls on his men, beseeching them with tears not to hand over himself or their commander Pompeius to the foe for punishment. A crowd quickly gathers at the general's headquarters. He demands that all should swear not to desert or betray the army and its officers, nor to take measures for their own safety apart from the rest. He first takes this oath himself, and also compels Afranius to take the same. Next come the military tribunes and centurions; the rank and file come forward and take the oath century by century. They issue orders that any soldier of Caesar who is in the company of one of their men should be brought forward by him. When produced they kill him publicly at the headquarters. But many of them are concealed by those who had entertained them, and are let go at night through the ramparts. Thus the intimidation employed by the generals, cruelty in punishment, and the obligation of their fresh oath removed all prospect of present surrender, changed the inclination of the soldiery, and brought matters back to the old condition of hostility.
Caesar gives orders that the men of the other side who had come into his camp at the time of the colloquy should be sought for with the utmost diligence and sent back. But out of their number several military tribunes and centurions remained with him of their own accord. These he afterwards held in high honour; centurions he restored to their former ranks, Roman knights to the post of tribune (Caes. B. Civ. 1.74-7).
Essentially what happened here is that, left to their own devices by their generals, the veterans of the Pompeian army, many of whom knew individuals in Caesar's army, began to intermingle with Caesar's troops, and vice versa. When the Pompeian generals returned, Afranius basically gives up. This is telling, as it shows how beholden a Roman general was to the loyalty of his troops, and it was not unusual for a mutinous army to dictate terms to their own general in this period, or even hand him over to the enemy (as happened to Lepidus against Octavian, and several others). The other Pompeian general, Petreius, is made of sterner stuff however (though both generals are very experienced), and with his most loyal troops drives any Caesarian troops that hand intermingled from his own camp, and then beseeches his own men "with tears" not to betray them to Caesar. In the end, and this is a mark of how well-respected he was as a general, Petreius' men sided with him and he formed them to swear the sacramentum, the military oath, all over again as they would have done when forced enrolled in their legions. The sacramentum was heavily utilised to bolster the loyalty of soldiers in this period, and it had generally become an oath of loyalty to a particular general rather to the state, and as can be seen, this did not necessarily prevent desertion. Having gotten his men in hand, only then does Petreius turn to threats to ensure the rest of his troops fell into line. Caesar, on the other, talks up his clementia, attempting to convince his reader that he did not need to rely on threats to secure the loyalty of his own troops, but we must suspect that the matter was not so simple, as Caesar was in as much as danger as his opponents of having discipline totally break down.
This example was pretty typical of most civil wars in the last century of the Republic, and the possibility of desertion factored into the plans of generals throughout this period. Perhaps one of the more strategically meaningful examples of this is Antony's decision not to fight a land battle at Actium, as he probably did not trust his men to fight against Octavian. As we know, this ended with him attempting to escape the bottleneck his fleet was caught in, resulting in its total destruction, but if Antony was surer of the loyalty of his troops he would have been quite capable of leading his army to victory against Octavian and Agrippa.
Consequences for desertion could be harsh, but as seen in the above example, there was a fine line between maintaining discipline and annoying the men that a general needs on-side for him to be successful. That said, it is no coincidence that decimation came back into fashion in this period, whereas it had basically gone out of use before the Civil Wars. A combination of personal charisma, harsh discipline and the promise of reward, or as above, even tears and pleading if necessary, were necessary for a Late Republican general to maintain control of his army in periods of Civil War.
Let me know if this answers your question, I can provide more details and sources if you wish.
Sources:
Caesar, Bellum Civile. Trans A.G. Peskett, 1914. Loeb Classical Library (from lacuscurtius.org).
Goldberg, Charles. “Decimation in the Roman Republic,” The Classical Journal, Vol. 111, No.2, 2015, 141-64.
Gueye, Mariama. "La valeur du serment militaire dans les guerres civiles à Rome: l’exemple du conflit de 49-45 av. J.-C." Gerión, Vol. 33, 111-129.
Scullard, H.H. From the Gracchi to Nero: A History of Rome 133 B.C. to A.D. 68. London: Routledge, 2011.