The story of Tamar (Gen 38) interrupts the long narrative of Joseph, so we will discuss her first. Jacob's son Judah married a Canaanite woman and had three sons by her. The first son was Er, and he married Tamar, presumably a Canaanite. Er was sinful, so God killed him. According to a custom which also passed into later Israelite law (e.g., the book of Ruth), the second son was obligated to marry the widow, so as to preserve the line of the dead son. However, Onan, the second son, refused, and the Lord killed him too. This left only the third son, Shelah, but he was too young to marry. Judah told Tamar to wait for him to grow up.
Years passed, and Judah delayed the marriage of Shelah and Tamar. So Tamar took things into her own hands. She disguised herself as a harlot and got her father-in-law to lie with her. She took a pledge of payment from him. Three months later, when she was found to be pregnant, Judah was going to have her burned, until she presented the pledge. This worked as well as DNA does today in proving the fatherhood of her twin sons. Judah was ashamed, and acknowledged his guilt.
"She is more righteous than I" (Gen 38:26).
It sounds like material for a later Greek tragedy, however the sin that Judah admitted to was not incest but failure to provide a husband for Tamar.
There are four racial/gender overtones to this story:
a. the sons of Jacob took Canaanite wives. This represents a real cutting of ties with Mesopotamia. Jacob's grandsons, the heads of the tribes of Israel, were half-Canaanite.
b. a wife had certain marital rights, regardless of her racial origin. This woman was not a concubine or slave.
c. Tamar's sons were Perez and Zerah. Perez was an ancestor of David, and thus of Jesus.
d. the significant issue to God was not race but obedience. Two sons of Judah were killed by God for evil deeds, but a Canaanite woman performed an evil deed (harlotry) for the sake of a higher purpose (preservation of the family name), and was vindicated. In three ways -- race, sin (prostitution) and final justification -- Tamar was a precursor of Rahab (see Joshua 2).