Well think of the situation this way...
Kuma asked Vegapunk to input that order before he lost his personality. Thus, speaking in programming terms, Kuma was given a "command".
Now after losing his personality, the World Government needed some way to control Kuma and give him other missions. Though the method has not yet been revealed, it is most likely left to the capable hands of our good friend Vegapunk. This means that Kuma can either be given more missions or have missions cancelled. Defending Marineford was one of these missions.
So why did Kuma go to war? Let's go back to the programming thing I mentioned earlier. Kuma has been given multiple commands. So how do computers and cyborgs determine which commands to complete first? They prioritize them. The battle of Marineford was one that took great priority because it determined the direction the new age would go. Not wanting to lose, the WG needed as much power as it could get. Vegapunk, though highly respected in the WG, was still required to send Kuma into battle or else that would be treasonous. Even if his wish to defend the Strawhat ship was already inputted, Vegapunk could override it by placing a command that took priority. Thus, Kuma went to war.
After a command is completed, the one that is the new priority begins. Remember, Kuma only began to defend the ship AFTER the war. So you may now ask what happened between the events of Sabaody and the war?
Well if you read carefully, the time between fighting the strawhats and going to war is less than two weeks. It is probably safe to say that Vegapunk spent this time upgrading Kuma and preparing him for war. Kuma may have been given his commands during this time.
I hope I straightened out the issue for everyone here. Now to get back to my homework...