Gents,
Sweet Fanny Adams... the goddamned "Shoe Factory" myth again...
There was no "shoe factory" at Gettysburg and there was no supply depot either. What's more, the ANV in the person of Early knew both those things because he'd already marched through the town four days earlier foraging as he went.
Lee had recently learned that the Army of the Potomac had crossed the river it was named after and was marching north. Just as in their last invasion of the North, the ANV was scattered from hell to breakfast due to logistical concerns. Lee was determined not to get caught with a divided army as he had been earlier before Antietam, so he was looking to concentrate somewhere. Concentrating to the east was a better choice than concentrating to the west because it maintained a threat towards more US cities like Harrisburg, Baltimore, and Washington.
To concentrate to the east, Lee needed to pass two of his corps and their wagon trains through the South Mountain gaps before the Army of the Potomac came up. That move placed most of those corps on the Chamberstown-to-Gettysburg road as they moved through the gap, so Heth was dispatched to Gettysburg in order to control the road junctions there and give Lee the strategic room he needed.
At Gettyburg, Heth ran into Buford and, both because his "blood was up" and he first thought he was facing militia, he began the general engagement his orders told him avoid.
After that, it was a race between the ANV and AoP as to who could get to Gettysburg faster. Lee had to clear the mountain gaps or risk being penned up in them and Meade had to prevent Lee from using the road net centered on Gettysburg to escape Adams County and the advancing US army.
As Lee himself later wrote
"It had not been intended to deliver a general battle so far from our base unless attacked, but coming unexpectedly upon the whole Federal Army, to withdraw through the mountains with our extensive trains would have been difficult and dangerous..."
Lee had to attack at Gettysburg because he couldn't withdraw from Gettysburg. The roads back through the South Mountain gaps weren't sufficient to pass his entire army rapidly enough to avoid being defeated in detail.
If you want the battle at Gettysburg not to occur, either concentrate the ANV there earlier so that Lee move off or get the AoP there earlier so that Lee chooses not to pass through the South Mountain gaps.
Bill