Who had the sacred knowledge of Bhagavat Gita before Arjuna?
I am not sure but I listen that lord Shiva and lord Sun know it before Arjuna?
How did they come to know?
"Who had the sacred knowledge of Bhagavat Gita before Arjuna?"
May not be the current "Bhagavad Gita" as it is, but at least the core message about Yoga system was told before.
BG 4.1 - The Blessed Lord said I imparted this imperishable Yoga to Vivasvan, Vivasvan taught this to Manu, and Manu transmitted this to Ishavaku.
BG 4.2 - The king-sages knew this (yoga) which was received thus in regular succession. That Yoga, O destroyer of foes, is now lost owing to a long lapse of time.
Many translators have translated Vivasana as Sun.
"How did they come to know?"
Similar Qn was asked by Arjuna as well:
BG 4.4 - Arjuna said, 'Your birth was later, [whereas] the birth of Vivasvan was earlier. How am I to understand this that You instructed [him] in the beginning?'
Now, Krishna doesn't answer to that directly. But we have to infer from the below verse that, during any of his previous births, Krishna would have departed the knowledge of Yoga to Vivasana.
BG 4.5 - The Blessed Lord said O Arjuna, many lives of Mine have passed, and so have yours. I know them all, (but) you know not, O scorcher of enemies!
Note: All the translations by Gambhirananda
Krishna briefly describes it who learned the knowledge contained in the Bhagavad Gita previously in chapter 4 of the Bhagavad Gita:
I instructed this imperishable science of yoga to the sun-god, Vivasvān, and Vivasvān instructed it to Manu, the father of mankind, and Manu in turn instructed it to Ikṣvāku. This supreme science was thus received through the chain of disciplic succession, and the saintly kings understood it in that way. But in course of time the succession was broken, and therefore the science as it is appears to be lost. That very ancient science of the relationship with the Supreme is today told by Me to you because you are My devotee as well as My friend and can therefore understand the transcendental mystery of this science.
This is referring to the passing down of the Pancharatra tradition, and it's described in more detail in this chapter of the Shanti Parva of the Mahabharata:
During the seventh birth of Brahma due to the lotus, O king, that sprang from the navel of Narayana, this cult was once more declared by Narayana himself, unto the Grandsire of pure soul, the Creator of all the worlds, in the beginning of this Kalpa. The Grandsire gave it in days of yore to Daksha (one of his sons created by a fiat of his will). Daksha, in his turn, imparted it to the eldest of all the sons of his daughters, O monarch, viz., Aditya who is senior in age to Savitri. From Aditya, Vivaswat obtained it. In the beginning of the Treta Yuga, Vivaswat imparted the knowledge of this cult to Manu. Manu, for the protection and support of all the worlds, then gave it to his son Ikshaku. Promulgated by Ikshaku, that cult over-spreads the whole world. When the universal destruction comes, it will once more return to Narayana and be merged in Him. The religion which is followed and practised by the Yatis, has, O best of kings, been narrated to thee before this in the Hari Gita, with all its ordinances in brief. The celestial Rishi Narada got it from that Lord of universe, viz., Narayana himself, O king, with all its mysteries and abstract of details. Thus, O monarch, this foremost of cults is primeval and eternal. Incapable of being comprehended with ease and exceedingly difficult of being practised, it is always upheld by persons wedded to the attribute of Sattwa.
The Hari Gita is another name for the Bhagavad Gita, and as the passage says it encapsulates the teachings of Pancharatra.