That is not so difficult.
Von Bayern = main lineage
In Bayern = collateral lineages
But... These collateral lineages have become extinct. The title Herzog in Bayern would have ended was it not that the previous Herzog in Bayern adopted his second cousin Max as his legal son, heir and successor. This Max is the younger brother of the current Herzog von Bayern, Franz.
But things will be complicated again: Franz, Herzog von Bayern has no issue. His brother Max, Herzog in Bayern is his heir. So once again the title Herzog in Bayern is on the verge of extinction.
Or the new Duke will combine it: Max, Herzog von und in Bayern, uniting both titles in his person.
I think they will stop to use the tile of a "duke in Bavaria".
For those interested in history: the House of Wittelsbach, originally from the villages of Ober- and Unterwittelsbach north of Augsburg in Bavaria, had several lines. The two most important reigned in Bavaria as duke of Bavaria and the other line in the Palatinate as Prince Elector of the Palatinate.
In winter 1919/1620, shortly after the beginning of The Great War of 30 years (which started with the defenestration of Prague), Frederick V. of the Palatinate made the grave error to head the protestant try to take Bohemia back from the catholic Habsburgs of Austria and let himself be voted for and crowned king of Bohemia. The Habsburgs threw him out and he henceforth lived with his wife Elisabeth Stuart, only daughter of James I. of England and VI. of Scotland (a granddaughter of Mary Stuart) in Den Haag, having lost the Palatinate to the Habsburg-troups from Spain and the office of Prince Elector to his Wittelsbach-cousin Maximilian I. of Bavaria. After Maximilian's death, the office of Prince Elector returned to the Palatinian Wittelsbach and a new one was created for the Bavarian Wittelsbachs.
Frederick's and Elisabeth Stuart's daughter Sophie of the Palatinate btw. was married to the Prince Elector of Hannover and protestant her line was elected by the English and Scottish parliament to inherit the Stuart throne.
In 1766 it was clear that the Bavarian lines of the Wittelsbachs would die out with Maximilian III. Joseph, Prince Elector and Duke of Bavaria. meanwhile the Palatinian line of Pfalz-Neuburg (in Bavaria, but they owned the Palatinate) had died out and Karl Theodor, Duke of Pfalz-Sulzbach had become the new Prince Elector. So Max III. Joseph and Karl Theodor signed a treaty declaring the unity of the Palatinate and Bavaria and named Munich as the capital. So when Max died in 1777, Karl Theodor inherited Bavaria and moved to Munich.
One of the first things he did was grant his Palatinian relatives, who had never held Bavarian titles, ducal titles of Bavaria. But not the traditional "von"-title, but the new title "in" Bavaria in addition to their Palatinian titles. So when Duke Max in Bavaria, Count Palatine of Palatinate-Birkenfeld-Gelnhausen married Princess Ludovika of Bavaria, daughter of his cousin Max, the successor of Karl Theodor and by Napoleon's grace king Max I. Joseph of Bavaria, the whole "in Bavaria"-branch of the Wittelsbachs were given the style of Royal Highnesses in addition to their ducal titles.
One of their children was Elisabeth - HRH Elisabeth, duchess in Bavaria, who married Emperor Franz Joseph of Austria.
So the ducal title of "in Bavaria" is the lesser of the two ducal titles in Bavaria. There is no title of Prince/Princess in Bavaria as the original Palatinian titles were Count/Countess of the Palatinate (Pfalzgraf/-gräfin). Max who is a born "Prinz von Bayern" from the Ducal later Royal main line only let himself be adopted to be able to inherit the wealth of the "in Bavaria"-branch of the family, I don't think the title mattered to him as if is lower in rank than the title he was born with. AFAIK his daughters hold both titles (or did before they married): princess "von" and duchess "in" but the "von" members are closer to the throne, so their title is higher in rank.
If you have any further questions, I'll try to answer them.