share project code to another project as git submodule

สร้าง project สำหรับจะถูกเรียกใช้เป็น submodule

และ push ให้มี branch ที่ต้องการก่อน เช่น main

  • หากมี subscription ให้สร้าง access token
  • หากไม่มี subscription ให้สร้าง user ที่จะใช้เป็นที่แชร์รหัสผ่านได้มา โดย user นี้จะมีการสร้าง pat (personal access token) ตามรายละเอียดทีนี่

เพิ่ม submodule

ทำใน project ปลายทาง

git submodule init
git submodule add https://user:user_pat@gitlab.com/path/to/submodule/project.git submodule_name

หากทำสำเร็จ git จะไป clone submodule project และสร้าง folder submodule_name และทำการ link กับ git repo ของ project submodule ซึ่งเราจะเห็นว่า vs code แยกเป็น module หลัก และ submodule อื่นๆ ดังในภาพ

และจะเห็นว่ามีไฟล์ .gitmodules ซึ่งเป็นการเก็บข้อมูลของ submodule ของ project ไว้ ให้ commit file นี้เข้า repo ด้วย

อย่าลืม commit submodule ก่อนแก้ไข project หลัก และทำการ update/sync submodule บ่อยๆ ด้วย