Shortly after the Army of Northern Virginia won a major victory over the Army of the Potomac at the Battle of Chancellorsville (April 30 – May 6, 1863), Robert E. Lee decided upon a second invasion of the North (the first was the unsuccessful Maryland Campaign of September 1862, which ended in the bloody Battle of Antietam). Such a move would upset Federal plans for the summer campaigning season and possibly reduce the pressure on the besieged Confederate garrison at Vicksburg. It would allow the Confederates to live off the bounty of the rich Northern farms while giving war-ravaged Virginia a much needed rest. In addition, Lee's 72,000-man army could threaten Philadelphia, Baltimore, and Washington, and possibly strengthen the growing peace movement in the North.

Thus, on June 3, Lee's army began to shift northward from Fredericksburg, Virginia. In order to attain more efficiency in his command, Lee had reorganized his two large corps into three new corps. Lt. Gen. James Longstreet retained command of his First Corps. The old corps of deceased Thomas J. "Stonewall" Jackson was divided into two, with the Second Corps going to Lt. Gen. Richard S. Ewell and the new Third Corps to Lt. Gen. A.P. Hill. The Cavalry Division was commanded by Maj. Gen. J.E.B. Stuart.

The Union Army of the Potomac, under Maj. Gen. Joseph Hooker, consisted of seven infantry corps, a cavalry corps, and an Artillery Reserve, for a combined strength of about 94,000 men. However, President Lincoln replaced Hooker with Maj. Gen. George Gordon Meade, a Pennsylvanian, because of Hooker's defeat at Chancellorsville and his timid response to Lee's second invasion north of the Potomac River.

The first major action of the campaign took place on June 9 between the opposing cavalry forces at Brandy Station, near Culpeper, Virginia. The 9,500 Confederate cavalrymen under Stuart were surprised by Maj. Gen. Alfred Pleasonton's combined arms force of two cavalry divisions (8,000 troopers) and 3,000 infantry, but Stuart eventually repulsed the Union attack. The inconclusive battle, the largest predominantly cavalry engagement of the war, proved that for the first time, the Union horse soldier was equal to his Southern counterpart.

By mid-June, the Army of Northern Virginia was poised to cross the Potomac River and enter Maryland. After defeating the Federal garrisons at Winchester and Martinsburg, Ewell's Second Corps began crossing the river on June 15. Hill's and Longstreet's corps followed on June 24 and June 25. Hooker's army pursued, keeping between the U.S. capital and Lee's army. The Federals crossed the Potomac from June 25 to June 27.

Lee gave strict orders to his army to minimize any negative impacts on the civilian population. Food, horses, and other supplies were generally not seized outright, although quartermasters reimbursing Northern farmers and merchants using Confederate money were not well received. Various towns, most notably York, Pennsylvania, were required to pay indemnities in lieu of supplies, under threat of destruction. The most controversial of the Confederate actions during the invasion was the seizure of some 40 northern African Americans, a few of whom were escaped slaves but most freemen. They were sent south into slavery under guard.

On June 26, elements of Maj. Gen. Jubal Early's division of Ewell's Corps occupied the town of Gettysburg after chasing off newly raised Pennsylvania militia in a series of minor skirmishes. Early laid the borough under tribute but did not collect any significant supplies. Soldiers burned several railroad cars and a covered bridge, and they destroyed nearby rails and telegraph lines. The following morning, Early departed for adjacent York County.

Meanwhile, in a controversial move, Lee allowed J.E.B. Stuart to take a portion of the army's cavalry and ride around the east flank of the Union army. Lee's orders gave Stuart much latitude, and both generals share the blame for the long absence of Stuart's cavalry, as well as for the failure to assign a more active role to the cavalry left with the army. Stuart and his three best brigades were absent from the army during the crucial phase of the approach to Gettysburg and the first two days of battle. By June 29, Lee's army was strung out in an arc from Chambersburg (28 miles (45 km) northwest of Gettysburg) to Carlisle (30 miles (48 km) north of Gettysburg) to near Harrisburg and Wrightsville on the Susquehanna River.

In a dispute over the use of the forces defending the Harpers Ferry garrison, Hooker offered his resignation, and Abraham Lincoln and General-in-Chief Henry W. Halleck, who were looking for an excuse to get rid of him, immediately accepted. They replaced him early on the morning of June 28 with Maj. Gen. George Gordon Meade, at the time commander of the V Corps.

On June 29, when Lee learned that the Army of the Potomac had crossed the eponymous river, he ordered a concentration of his forces around Cashtown, located at the eastern base of South Mountain and eight miles (13 km) west of Gettysburg. On June 30, while part of Hill's Corps was in Cashtown, one of Hill's brigades, North Carolinians under Brig. Gen. J. Johnston Pettigrew, ventured toward Gettysburg. The memoirs of Maj. Gen. Henry Heth, Pettigrew's division commander, claimed that he sent Pettigrew to search for supplies in town—especially shoes.

When Pettigrew's troops approached Gettysburg on June 30, they noticed Union cavalry under Brig. Gen. John Buford arriving south of town, and Pettigrew returned to Cashtown without engaging them. When Pettigrew told Hill and Heth about what he had seen, neither general believed that there was a substantial Federal force in or near the town, suspecting that it had been only Pennsylvania militia. Despite General Lee's order to avoid a general engagement until his entire army was concentrated, Hill decided to mount a significant reconnaissance in force the following morning to determine the size and strength of the enemy force in his front. Around 5 a.m. on Wednesday, July 1, two brigades of Heth's division advanced to Gettysburg.