Peter was very vocal about never denying the Lord, and his actions proved that he meant what he said. There were only two swords among the disciples, yet Peter was willing to take on the six hundred soldiers. This meant certain death or imprisonment.
Peter wanted to stand with the Lord, but he was not prepared spiritually. He was still strong in his own ability. If the battle would have been in the physical realm, Peter would have fought to the death as he proved here. But when Jesus told Peter to put up his sword and not resist with his physical power, Peter was confused.
Peter only knew how to rely on the flesh. When Jesus refused to allow Peter to fight with his sword, he was defenseless. If he would have been praying with Jesus, as instructed, Peter would have been built up spiritually and able to stand with Jesus spiritually without denying Him.
Likewise, we may sincerely desire to never deny our Lord, but it takes more than desire; it takes preparation. We all have been taught how to rely on ourselves, but we have to learn anew how to be strong in the Lord and in the power of His might (Ephesians 6:10). Just as in the physical realm, where muscles have to be exercised to become strong, so we have to exercise ourselves unto godliness (1 Timothy 4:7).