He gained fame at the Battle of Gettysburg for taking the initiative to piece together a line of guns that greatly contributed to the Union victory.
He soon returned home and raised the 6th Maine Battery, which first saw action at the battles of Cedar Mountain and Sulphur Springs in Western Virginia.
On July 2, the second day of the battle of Gettysburg, McGilvery discovered a wide and undefended gap in the Union line along the southern end of Cemetery Ridge, north of Little Round Top.
Initially without infantry support, McGilvery's "Plum Run line" of fieldpieces was instrumental in halting the final Confederate advance toward the Union center.
On July 3, the lengthy artillery line assisted in the repulse of Pickett's Charge, and in particular stopped the supporting attack of Confederate brigades under Cadmus Wilcox and David Lang.