In the early Summer of 1862 the Confederacy was in serious trouble. Union General George B. McClellan was executing his plan to capture the Confederate capital by invasion up the Virginia peninsula, and had advanced his great Army of the Potomac (AOP) to the 'gates of Richmond'. Just to the west, General John Pope and his Federal Army of Virginia, who were to drive on Richmond from the area of Fredricksburg, were also a major threat. These armies were both in Virginia, and each greatly outnumbered the forces defending Richmond; the fall of that place seemed almost inevitable.
While Pope was largely neutralized by the forces of Confederate General T. J. 'Stonewall' Jackson in the Shenandoah Valley, McClellan was steadily advancing.
The first major clash of the Peninsula Campaign occurred just east of Richmond at the Battle of Fair Oaks (or Seven Pines) on May 31st and June 1st, 1862. The battle was not a decisive victory for either side. The most significant outcome, however, was that Confederate commander Joseph E. Johnston was wounded in the fighting, and was relieved by General Robert E. Lee. General Johnston would later say that his wounding was perhaps the best thing ever to happen to the Confederacy.
While McClellan pondered his next move, Lee quickly united his small force defending Richmond with that of Jackson from the Valley, renamed it the Army of Northern Virginia (ANV), and began to turn the situation upside down.
Beginning on June 25th, Lee repeatedly attacked the much larger Army of the Potomac in a series of engagements known collectively as the Battles of the Seven Days. None of the battles were Confederate victories, but they kept McClellan off-balance and retreating. Lee and his ANV pushed the AOP back down the Peninsula to Harrison's Landing on the James. The Federal invasion was effectively over. Soon after, McClellan was ordered back to Washington.
General Lee then sent part of his army under Jackson to deal with Pope. Between the 9th and 28th of August, Jackson ran Pope ragged, baffling and harrassing him. On the 29th and 30th Pope and Jackson, quickly reinforced by the remainder of the ANV, met on the old Bull Run battlefield near Manassas. There at the Battle of Second Manassas Pope was thoroughly defeated, and sent running back into the defenses of Washington. Virginia was clear of the invader.