Why did God command killing in the Old Testament?

He didn’t.

People in those days - the storytellers of the OT - simply thought he did.

It’s all about perspective. 

From the point of view of an ancient person, everything, good or bad, was attributed to the gods.

Plentiful harvest? The gods must be pleased.

Famine or pestilence? The gods must be angry.

It was a very black and white, simplistic, worldview.

When you consider this primitive worldview, with no full revelation yet of God’s true loving nature via Christ, it is easy to see how events could be misinterpreted.

A catastrophic flood would be interpreted as God punishing the wicked.

An escape from slavery would be interpreted as God saving the righteous.

Now, with Christ’s incarnation, and his revelation that God is love, we come to see more clearly that all these calamities are in fact the doings of human beings and the side effects of sin - not God’s doings. 

Yes, the Hebrews had an inspired sense of morality that set them apart from other nations, direct from God himself, but it was far from complete. It was growing. God’s only means of communication with them, due to the rift of sin, was through inspiration. Obscure prophetic visions, speakings to the heart, etc. (when God speaks in the Bible, I do not think it is literal). It would take time, development, and for Jesus to come and completely reveal the ultimate morality and true nature of things, for the full picture of God to be revealed. When Jesus arrived, he was constantly saying “you’ve heard it said that… but I say…” to correct their misunderstandings. The fact that the Jews misinterpreted the messiah as being a war hero who would defeat the oppressors in battle shows again how hazy their view of God was. 

When Jesus arrived however, he said love your enemies, not destroy them. So the traditional stories of the Old Testament about God smiting his enemies were erroneous. Not falsities, but misattributions. God smiting we now see is really man bringing calamity on himself, directly, or indirectly through his own folly, or the unruly natural world messed up through sin. God was always there working in the background with the Hebrews, preparing the way for Christ, but probably not in ways they thought.

 What the ancients thought was good and from God because it was beneficial to them, such as victories in war or enemies dying, actually wasn’t from God at all. With Jesus’ revelation and teachings, we can now see in hindsight how the Old Testament image of the nature of God was obscured. 

Jesus revealed the true nature of God - that he is love. 

The books in the Old Testament were oral tradition, before they were ever written down, passed down by ancient peoples who were inspired by the One True God, but definitely fallible and subject to their historical context and limited understandings.

Have you ever heard about someone before you meet them, had preconceived ideas, and then get a new fuller impression once you actually do meet them? Well so it is with God.

The challenge is, the OT hasn’t been rewritten to reflect  our new perception of things. That is why we read the OT with context in mind, and always in light of the NT. 

If we understand that God is Love, and are familiar with Jesus’ revelation, we can easily see in the OT what is actually from God, and what is from man attributed to God.