1 Sam 17 tells us about one of the many wars between the Israelites and the Philistines. In this war, during the reign of king Saul, the Philistines have a champion called Goliat, who is described as follows:
(ד) וַיֵּצֵ֤א אִֽישׁ־הַבֵּנַ֙יִם֙ מִמַּחֲנ֣וֹת פְּלִשְׁתִּ֔ים גׇּלְיָ֥ת שְׁמ֖וֹ מִגַּ֑ת גׇּבְה֕וֹ שֵׁ֥שׁ אַמּ֖וֹת וָזָֽרֶת׃ (ה) וְכ֤וֹבַע נְחֹ֙שֶׁת֙ עַל־רֹאשׁ֔וֹ וְשִׁרְי֥וֹן קַשְׂקַשִּׂ֖ים ה֣וּא לָב֑וּשׁ וּמִשְׁקַל֙ הַשִּׁרְי֔וֹן חֲמֵשֶׁת־אֲלָפִ֥ים שְׁקָלִ֖ים נְחֹֽשֶׁת׃ (ו) וּמִצְחַ֥ת נְחֹ֖שֶׁת עַל־רַגְלָ֑יו וְכִיד֥וֹן נְחֹ֖שֶׁת בֵּ֥ין כְּתֵפָֽיו׃ (ז) (וחץ) [וְעֵ֣ץ] חֲנִית֗וֹ כִּמְנוֹר֙ אֹֽרְגִ֔ים וְלַהֶ֣בֶת חֲנִית֔וֹ שֵׁשׁ־מֵא֥וֹת שְׁקָלִ֖ים בַּרְזֶ֑ל וְנֹשֵׂ֥א הַצִּנָּ֖ה הֹלֵ֥ךְ לְפָנָֽיו׃
(4) A champion of the Philistine forces stepped forward; his name was Goliath of Gath, and he was six cubits and a span tall. (5) He had a bronze helmet on his head, and wore a breastplate of scale armor, a bronze breastplate weighing five thousand shekels. (6) He had bronze greaves on his legs, and a bronze javelin [slung] from his shoulders. (7) The shaft of his spear was like a weaver’s bar, and the iron head of his spear weighed six hundred shekels; and the shield-bearer marched in front of him.
Goliat is killed by David, who at that time is still a young shepherd, unskilled in battle:
וַיֶּחֱזַ֨ק דָּוִ֤ד מִן־הַפְּלִשְׁתִּי֙ בַּקֶּ֣לַע וּבָאֶ֔בֶן וַיַּ֥ךְ אֶת־הַפְּלִשְׁתִּ֖י וַיְמִתֵ֑הוּ וְחֶ֖רֶב אֵ֥ין בְּיַד־דָּוִֽד׃
Thus David bested the Philistine with sling and stone; he struck him down and killed him. David had no sword;
At the end of the book of Samuel we hear of more wars between the Philistines and Israel, during the reign of king David:
(טו) וַתְּהִי־ע֧וֹד מִלְחָמָ֛ה לַפְּלִשְׁתִּ֖ים אֶת־יִשְׂרָאֵ֑ל וַיֵּ֨רֶד דָּוִ֜ד וַעֲבָדָ֥יו עִמּ֛וֹ וַיִּלָּחֲמ֥וּ אֶת־פְּלִשְׁתִּ֖ים וַיָּ֥עַף דָּוִֽד׃ (טז) (וישבו) [וְיִשְׁבִּ֨י] בְּנֹ֜ב אֲשֶׁ֣ר ׀ בִּילִידֵ֣י הָרָפָ֗ה וּמִשְׁקַ֤ל קֵינוֹ֙ שְׁלֹ֤שׁ מֵאוֹת֙ מִשְׁקַ֣ל נְחֹ֔שֶׁת וְה֖וּא חָג֣וּר חֲדָשָׁ֑ה וַיֹּ֖אמֶר לְהַכּ֥וֹת אֶת־דָּוִֽד׃ (יז) וַיַּֽעֲזׇר־לוֹ֙ אֲבִישַׁ֣י בֶּן־צְרוּיָ֔ה וַיַּ֥ךְ אֶת־הַפְּלִשְׁתִּ֖י וַיְמִתֵ֑הוּ אָ֣ז נִשְׁבְּעוּ֩ אַנְשֵׁי־דָוִ֨ד ל֜וֹ לֵאמֹ֗ר לֹא־תֵצֵ֨א ע֤וֹד אִתָּ֙נוּ֙ לַמִּלְחָמָ֔ה וְלֹ֥א תְכַבֶּ֖ה אֶת־נֵ֥ר יִשְׂרָאֵֽל׃ {פ} (יח) וַֽיְהִי֙ אַֽחֲרֵי־כֵ֔ן וַתְּהִי־ע֧וֹד הַמִּלְחָמָ֛ה בְּג֖וֹב עִם־פְּלִשְׁתִּ֑ים אָ֣ז הִכָּ֗ה סִבְּכַי֙ הַחֻ֣שָׁתִ֔י אֶת־סַ֕ף אֲשֶׁ֖ר בִּילִדֵ֥י הָֽרָפָֽה׃ {ס} (יט) וַתְּהִי־ע֧וֹד הַמִּלְחָמָ֛ה בְּג֖וֹב עִם־פְּלִשְׁתִּ֑ים וַיַּ֡ךְ אֶלְחָנָן֩ בֶּן־יַעְרֵ֨י אֹרְגִ֜ים בֵּ֣ית הַלַּחְמִ֗י אֵ֚ת גׇּלְיָ֣ת הַגִּתִּ֔י וְעֵ֣ץ חֲנִית֔וֹ כִּמְנ֖וֹר אֹרְגִֽים׃ {ס} (כ) וַתְּהִי־ע֥וֹד מִלְחָמָ֖ה בְּגַ֑ת וַיְהִ֣י ׀ אִ֣ישׁ (מדין) [מָד֗וֹן] וְאֶצְבְּעֹ֣ת יָדָיו֩ וְאֶצְבְּעֹ֨ת רַגְלָ֜יו שֵׁ֣שׁ וָשֵׁ֗שׁ עֶשְׂרִ֤ים וְאַרְבַּע֙ מִסְפָּ֔ר וְגַם־ה֖וּא יֻלַּ֥ד לְהָרָפָֽה׃ (כא) וַיְחָרֵ֖ף אֶת־יִשְׂרָאֵ֑ל וַיַּכֵּ֙הוּ֙ יְה֣וֹנָתָ֔ן בֶּן־[שִׁמְעָ֖ה] (שמעי) אֲחִ֥י דָוִֽד׃ (כב) אֶת־אַרְבַּ֥עַת אֵ֛לֶּה יֻלְּד֥וּ לְהָרָפָ֖ה בְּגַ֑ת וַיִּפְּל֥וּ בְיַד־דָּוִ֖ד וּבְיַ֥ד עֲבָדָֽיו׃ {פ}
(15) Again war broke out between the Philistines and Israel, and David and the men with him went down and fought the Philistines; David grew weary, (16) and Ishbi-benob tried to kill David.—He was a descendant of the Raphah; his bronze spear weighed three hundred shekels and he wore new armor.— (17) But Abishai son of Zeruiah came to his aid; he attacked the Philistine and killed him. It was then that David’s men declared to him on oath, “You shall not go with us into battle any more, lest you extinguish the lamp of Israel!” (18) After this, fighting broke out again with the Philistines, at Gob; that was when Sibbecai the Hushathite killed Saph, a descendant of the Raphah. (19) Again there was fighting with the Philistines at Gob; and Elhanan son of Jaare-oregim the Bethlehemite killed Goliath the Gittite, whose spear had a shaft like a weaver’s bar. (20) Once again there was fighting, at Gath. There was a giant of a man, who had six fingers on each hand and six toes on each foot, twenty-four in all; he too was descended from the Raphah. (21) When he taunted Israel, Jonathan, the son of David’s brother Shimei, killed him. (22) Those four were descended from the Raphah in Gath, and they fell by the hands of David and his men.
In this list four Philistine champions are mentioned, and one of them is Goliat. But according to this text Goliat is not killed by David, but by one of his "mighty men", Elchanan.
The tradition tries to solve this contradiction in different ways. Solution one we find already in Tenach, in I Chronicles 20 where the same list is given as in I Samuel 21:
(ד) וַֽיְהִי֙ אַחֲרֵי־כֵ֔ן וַתַּעֲמֹ֧ד מִלְחָמָ֛ה בְּגֶ֖זֶר עִם־פְּלִשְׁתִּ֑ים אָ֣ז הִכָּ֞ה סִבְּכַ֣י הַחֻשָׁתִ֗י אֶת־סִפַּ֛י מִילִידֵ֥י הָרְפָאִ֖ים וַיִּכָּנֵֽעוּ׃ (ה) וַתְּהִי־ע֥וֹד מִלְחָמָ֖ה אֶת־פְּלִשְׁתִּ֑ים וַיַּ֞ךְ אֶלְחָנָ֣ן בֶּן־[יָעִ֗יר] (יעור) אֶת־לַחְמִי֙ אֲחִי֙ גׇּלְיָ֣ת הַגִּתִּ֔י וְעֵ֣ץ חֲנִית֔וֹ כִּמְנ֖וֹר אֹרְגִֽים׃ {ס} (ו) וַתְּהִי־ע֥וֹד מִלְחָמָ֖ה בְּגַ֑ת וַיְהִ֣י ׀ אִ֣ישׁ מִדָּ֗ה וְאֶצְבְּעֹתָ֤יו שֵׁשׁ־וָשֵׁשׁ֙ עֶשְׂרִ֣ים וְאַרְבַּ֔ע וְגַם־ה֖וּא נוֹלַ֥ד לְהָרָפָֽא׃ (ז) וַיְחָרֵ֖ף אֶת־יִשְׂרָאֵ֑ל וַיַּכֵּ֙הוּ֙ יְה֣וֹנָתָ֔ן בֶּן־שִׁמְעָ֖א אֲחִ֥י דָוִֽיד׃ (ח) אֵ֛ל נוּלְּד֥וּ לְהָרָפָ֖א בְּגַ֑ת וַיִּפְּל֥וּ בְיַד־דָּוִ֖יד וּבְיַד־עֲבָדָֽיו׃ {פ}
(4) After this, fighting broke out with the Philistines at Gezer; that was when Sibbecai the Hushathite killed Sippai, a descendant of the Rephaim, and they were humbled. (5) Again there was fighting with the Philistines, and Elhanan son of Jair killed Lahmi, the brother of Goliath the Gittite; his spear had a shaft like a weaver’s beam. (6) Once again there was fighting at Gath. There was a giant of a man who had twenty-four fingers [and toes], six [on each hand] and six [on each foot]; he too was descended from the Raphah. (7) When he taunted Israel, Jonathan son of David’s brother Shimea killed him. (8) These were descended from the Raphah in Gath, and they fell by the hands of David and his men.
The list in I Chronicles differs on several points from the list in II Samuel, and one of the differences is that Elhanan does not kill Goliat, but his brother Lahmi.
A second solution is given by the Rid (R. Yeshayah of Trani, 1165 – 1240, Italy):
רי"ד שמואל ב כ"א
(יט) אלחנן בן יערי אורגים בית הלחמי את גלית הגתי [...] וגלית אחר הוא זה
"Elhanan son of Jaare-oregim the Bethlehemite killed Goliath the Gittite" - this is another Goliat.
Rashi, following Targum and midrash, has another approach;
אלחנן. דוד:
Elchanan. David.
וַהֲוַת עוֹד קְרָבָא בְּגוֹב עִם פְּלִשְׁתָּאֵי וּקְטַל דָוִד בַּר יִשַׁי מְחֵי פָּרוֹכֶת בֵּית מַקְדְשָׁא דְמִבֵּית לֶחֶם יַת גָלְיַת גִתָּאָה וְאָעָא דְמוּרְנִיתֵיהּ כְאַכְסַן דְגַרְדָאִין:
... David the son of Jesse ... killed Goliat the Gittite ...
[...](שמואל ב כא, יט): וַיַּךְ אֶלְחָנָן, זֶה דָּוִד שֶׁחֲנָנוֹ הַקָּדוֹשׁ בָּרוּךְ הוּא, [...]
[...] “Elḥanan slew [Goliath the Gitite]” (II Samuel 21:19) – this is David, to whom the Holy One blessed be He was gracious [...]
According to this approach Elchanan is another name for David. And why was he called by that name? Because God [El] was gracious (chanan) to him.
Wikipedia sums up the modern view of the problem:
"Scholars today believe that the original listed killer of Goliath was Elhanan, son of Jair,[2] and that the authors of the Deuteronomic history changed the original text to credit the victory to the more famous character David.[3][4]"
[2] Finkelstein & Silberman 2007, pp. 2, 57.
[3] Halpern 2003, p. 8.
[4] Finkelstein & Silberman 2007, p. 196.
More interesting than the question of the historical truth of the story is the question why the writer of the book of Samuel tells the story of the killing of Goliat twice. Can we learn something from that?
Let's look again at the two versions of the story and compare them.
The version in II Samuel 21 is short and factual:
(יט) וַתְּהִי־ע֧וֹד הַמִּלְחָמָ֛ה בְּג֖וֹב עִם־פְּלִשְׁתִּ֑ים וַיַּ֡ךְ אֶלְחָנָן֩ בֶּן־יַעְרֵ֨י אֹרְגִ֜ים בֵּ֣ית הַלַּחְמִ֗י אֵ֚ת גׇּלְיָ֣ת הַגִּתִּ֔י וְעֵ֣ץ חֲנִית֔וֹ כִּמְנ֖וֹר אֹרְגִֽים׃ {ס}
(19) Again there was fighting with the Philistines at Gob; and Elhanan son of Jaare-oregim the Bethlehemite killed Goliath the Gittite, whose spear had a shaft like a weaver’s bar.
Goliat is one of the Philistine champions who fought against Israel and were killed by one of David's heroes. The version in II Samuel is no more than an excerpt of the war history of David's reign.
But the book of Samuel is not an historical book, but a prophetic one. And that becomes clear when we look at the much longer and detailed version of the story in I Samuel 17. The key verse is:
(מה) וַיֹּ֤אמֶר דָּוִד֙ אֶל־הַפְּלִשְׁתִּ֔י אַתָּה֙ בָּ֣א אֵלַ֔י בְּחֶ֖רֶב וּבַחֲנִ֣ית וּבְכִיד֑וֹן וְאָנֹכִ֣י בָֽא־אֵלֶ֗יךָ בְּשֵׁם֙ יְהֹוָ֣ה צְבָא֔וֹת אֱלֹהֵ֛י מַעַרְכ֥וֹת יִשְׂרָאֵ֖ל אֲשֶׁ֥ר חֵרַֽפְתָּ׃
(45) David replied to the Philistine, “You come against me with sword and spear and javelin; but I come against you in the name of the LORD of Hosts, the God of the ranks of Israel, whom you have defied.
This is a prophetic utterance. Not strength against strength, as in the version in II Samuel, but
לֹ֤א בְחַ֙יִל֙ וְלֹ֣א בְכֹ֔חַ כִּ֣י אִם־בְּרוּחִ֔י אָמַ֖ר יהוה צְבָאֽוֹת׃
Not by might, nor by power, but by My spirit—said the LORD of Hosts.