Remember that II Samuel 17:25 tells us that David replaced Joab with Amasa. To put that in perspective, he gave Joab's position to a recently defeated general. It was a severe slighting of Joab for David to replace him with his recent enemy. But, Joab would not surrender so easily. When David sent Amasa out against an upstart named Sheba, Joab was bitterly upset.
When the tribes of Israel got into a feud with the tribe of Judah over which group had the closest connection with the old king, Sheba took that opportunity to exert whatever influence he had to lead another rebellion against King David. To stop this, David called on Amasa. When he took too long, David called on Abishai (Joab's brother) to do the job. Really, Joab took the men who were loyal to him and took off on his own to accomplish the mission before the other 2 could do it.
In the process or pursuing Sheba, Joab came upon Amasa, and, under the pretense of a kind greeting, Joab killed Amasa. That's one way of making sure you are the winner. Kill your competition. Obviously, Joab was wrong in doing it. In the long run, he paid for his actions. But, for the time being he got what he was after. He took Sheba's head to David and was thereby restored to his position as military chief. It seems that David was rather pragmatic when it came to appointing military leaders (see II Samuel 5:8).