Dear @Midorrion,
Thank you for your question and welcome to the Wefugees Community!
To apply for a residence permit to live with your German child (under § 28 Abs. 1 Nr. 3 AufenthG), the most important and tricky part will be to have your paternity acknowledged (Vaterschaftsanerkennung). Good news is that you can start the process before your child's birth.
Here is how the process could **** like if your girlfriend's husband is cooperative: he can make a declaration to the Child Services (Jugendamt), stating that he is not the father. Only under this condition will you be able to declare yourself as the father of your girlfriend's child.
On her side, your girlfriend will need to make a declaration of consent (Zustimmungserklärung) and a declaration of shared custody (Sorgerechterklärung) at the Jugendamt, stating that she allows you to do the Vaterschaftsanerkennung (acknowledgement of parternity) and plans to share custody of your child with you.
Since you are abroad, the German Embassy in Morroco will be responsible for certifying your Vaterschaftsanerkennung and Sorgerechterklärung. For this, you will need to send them a series of documents per E-mail (copy of your girlfriend's "Mutterpass", her passport, proof of address and contact, the declarations she made at the Jugendamt, as well as her husband's declaration). You can find a list (in German) of the necessary documents on this webpage (Vaterschaftsanerkennung), and this one (Sorgerechtserklärung).
If your girlfriend's husband refuses to cooperate, she will have to go to Court to contest his parternity (so called Vaterschaftsanfechtung) before you can get on with the Vaterschaftsanerkennung.
Once you did both the Vaterschaftsanerkennung and the Sorgerechterklärung, you can apply for a family reunification visa at the German Embassy in Morroco. You will qualify for a 3 years family reunification residence permit, which you will have to apply for at the foreigner office upon your arrival in Germany.
Depending on the husband's willingness to cooperate, the whole process might take a long time, so I highly recommend your girlfriend and you to start with it as soon as possible, by contacting the Child services as well as the Embassy!
I hope this answer is helping - don't hesitate to add a comment if you need any further clarification!
All the best,
Safia