Genesis 20:1
And Abraham journeyed from thence toward the south country, and dwelled between Kadesh and Shur, and sojourned in Gerar. And Abraham journeyed from thence toward the south country, and dwelled between Kadesh and Shur, and sojourned in Gerar.
Genesis 12 to 26 records God's providential dealings with the family of Abraham as the chosen progenitor of the promised Seed, continuing the history of beginnings and the story of God's dealing with the Seed. Genesis 20 belongs to the continuing story of Abraham and his family in which Abraham's faith gradually develops. The pilgrim journey and sojourn among the Philistines at Gerar between Kadesh and Shur comes after the destruction of Sodom and Gomorrah and the account of the origins of Ammon and Moab, and before the birth of Isaac and the events that follow in chapter 21. Abimelech was king there, and he sent Abraham's household onward with rich gifts and goodwill.
Abraham had sojourned in Gerar where he had an experience somewhat like Isaac's later experience there. Abraham made a covenant with Abimelech that was respected during Abraham's lifetime. After his death the Philistines stopped up the wells he had dug.
The journey to Gerar exposes Abraham's lack of trust when he passed Sarah off as his sister. Though known as a man of faith, he did not always trust God. God used Abimelech there to administer a stern rebuke to Abraham.
At Gerar Abimelech coveted Abraham's wife and therefore took her for himself, and God directly instructed him about it. That act would have brought dire punishment upon Abimelech even though he did it unwittingly. Abimelech knew the prohibition against adultery. The episode in Genesis 20:1-9 shows that adultery was already regarded as a great sin and as guilt before God, even though no earlier express prohibition is recorded, and that the patriarchs knew it was wrong. Abimelech's coveting of Abraham's wife in Genesis 20:1-7 is a violation of the tenth commandment, and together with the adultery it proves that the law forbidding adultery and the law against coveting were already in force and known long before Sinai and long before Moses.