"didn't the reps lose the 1876 elections and the dems pulled the federal troops out of the south leadin to the risin of the klan and end of reconstruction"
Yes and no. The 1876 election ended up with both the Republican candidate (Hayes) and Democratic nominee (Tilden) short of the number of electoral votes they needed to win the presidency. (In Tilden's case, 1 electoral vote.) The Compromise of 1877 is what the unwritten agreement between the Republicans and Democrats ended up doing: Turning over disputed electoral votes, primarily in the the South, to Hayes. In the end, Hayes got one more electoral vote than he needed to win, while Tilden remained stalled 1 vote shy.
In return, the Reublicans ended Reconstruction and withdrew federal troops from the South. The KKK had been going earlier but it really took over after 1877. Soon you had legalized segregation on the basis of "race."
Essentially, the Republicans decided those electoral votes were more important to them than any promises they'd made to protect newly freed slaves from the not so tender mercies of their former owners.
It's tricky, trying to equate modern political parties with those back then, though.
Anyway, for many years the descendants of those freed slaves often voted Republican -- again, when they could vote or even register -- because the Democrats in the South represented repression.
The white South was basically a bastion of those reactionary Democrats until they saw some new possibilities in the 1964 presidential election (strong rightward swing by the GOP). But, especially, when Nixon's folks started peddling the "Southern Strategy." Then many of those Democrats became Republicans.