The American Civil War was one of the first major conflicts in which railroads played a significant part. Naturally, the highly industrialized North had many times over the rail capacity of the South. Since the vast majority of the battles found were in Southern Territory, the direct use of rails to move troops was limited by that capacity. Overall, the greatest usage by both countries was the movement of supplies rather than men.
But rails played a unique role in the Battle of Gettysburg. First of all, as the Army of the Potomac (AoP) began its trek north, the War Dept. began moving vast qualities of supplies to Westminster via Baltimore. Had they been needed, rail lines were available to move them farther west to Hagerstown and Frederick MD.
The ability of the Union to move men and equipment by rail was not lost on GEN Lee. The primary reason that he dispatched MG Early’s Division to York was to disrupt the rails lines that fed north to Harrisburg. He needed to prevent reinforcements from arriving there.
Although it played no actual role in the battle as it was fought, there was a spur line that ran from York into Gettysburg. The closest involvement of the railway in that battle was the fight over the ‘Railway Cut’ that was being dug to extend that spur westward.
Another interesting line of track ran west from Harrisburg through Carlisle then making a long sweeping arc to the south feeding into the Cumberland Valley. Lee turned his army east as they crested the tip of South Mtn so they never actually entered that valley. Had Lee continued farther north towards Carlisle and as some historians speculate to Dillsburg and Harrisburg, he might have found a use for that long spur to move supplies. The major limitation would have been locating sufficient rolling stock to carry what he needed.
Unfortunately for Meade, in the immediate aftermath of the battle, there were no convenient rail lines to use to try to leapfrog his forces ahead of the retreating Lee. Any such forces would have had to come out the 8 Union Corps maintaining the BALTO-WASHDC defense corridor and Lincoln was not about to further deplete his defenses.
Noted historian and author Scott Mingus Sr. has written a number of books about the Civil War in Pennsylvania, many of them about the importance and the involvement of the rail systems there.
Also student / author Cooper Wingert has written a number of books involving the Railways of Pennsylvania during this era.