22 At that time Abimelek and Phicol the commander of his forces said to Abraham, “God is with you in everything you do. 23 Now swear to me here before God that you will not deal falsely with me or my children or my descendants. Show to me and the country where you now reside as a foreigner the same kindness I have shown to you.”
24 Abraham said, “I swear it.”
25 Then Abraham complained to Abimelek about a well of water that Abimelek’s servants had seized. 26 But Abimelek said, “I don’t know who has done this. You did not tell me, and I heard about it only today.”
27 So Abraham brought sheep and cattle and gave them to Abimelek, and the two men made a treaty. 28 Abraham set apart seven ewe lambs from the flock, 29 and Abimelek asked Abraham, “What is the meaning of these seven ewe lambs you have set apart by themselves?”
30 He replied, “Accept these seven lambs from my hand as a witness that I dug this well.”
31 So that place was called Beersheba, because the two men swore an oath there.
32 After the treaty had been made at Beersheba, Abimelek and Phicol the commander of his forces returned to the land of the Philistines. 33 Abraham planted a tamarisk tree in Beersheba, and there he called on the name of the Lord, the Eternal God. 34 And Abraham stayed in the land of the Philistines for a long time.
Genesis contains several examples of what seem to be multiple versions of the same story. This is probably because the stories originally came from oral tradition and variants were floating around that got written down later. There are versions of the same thing happening to the same people (e.g. Hagar and Ishmael, as we have just seen). When these get woven into an overall narrative we get anachronisms, as with Ishmael being a baby when he was actually 14.
This is a different sort of repeat, where the same story happens to different people. As we shall see, Isaac eventually goes through the same rigamarole with Abimelech, and he names the well Beersheba, which seems odd since it had already been named by his father.
Later, in Deuteronomy, the "Philistines" become the perpetual enemies of the Hebrews. There are other problems, including the location of the Philistine nation, which is not the same. So the Talmudic scholars decided that these must not have been the same people after all, in spite of having the same name. More likely, of course, is that the history is just garbled, to the extent it has any basis in fact at all. There is a modern city named Beersheba, but it is impossible to say if it has any real continuity with Abraham's (or Isaac's) settlement.