เมื่อ contribute โค้ดให้แอป มัก commit บน feature branch ใน git คู่มือนี้ดูวิธีเก็บ commits ของโปรเจกต์ใน sync พร้อมblick สั้นๆ เกี่ยวกับ git และ GitHub
คิดว่า git เป็น command line tool ติดตามการเปลี่ยนแปลงโค้ด คำสั่งทั่วไป: git add, git commit, git push สำหรับ save changes เป็น version control
หลังใช้คำสั่งเหล่านี้ save การเปลี่ยนแปลงไฟล์ git push จะ save ไฟล์ไป code hosting platform เช่น GitHub GitHub เก็บไฟล์ใน repositories และให้ user แชร์โค้ดง่าย
Code hosting platforms อย่าง GitHub ทำให้ remote work บนโปรเจกต์ร่วมกันได้ เนื่องจากงานเทคโนโลยีย้ายสู่ remote work solutions เหล่านี้จึงสำคัญกว่าเดิม
เมื่อไหร่ควรใช้ git merge
ไม่ว่าจะทำงานบริษัทกับนักพัฒนาคนอื่น หรือ contribute open source projects สำหรับความสนุก จะใช้ git merge command สำคัญต้องรู้ว่าบริษัทหรือ open source project ยอมรับ commits อย่างไร ถามและอ่าน guidelines ก่อนทำต่อ
ปกติจะได้ task ทำ feature ของแอป จะ commit changes บน feature branch feature branch คือ branch ที่ "branch" ออกจาก master branch master branch คือที่ production code สุดท้ายอยู่
ขณะ development โค้ดบน feature branch ช่วยให้ master branch clean จาก bugs ที่เกิดระหว่าง development feature branch ควรตั้งชื่อตาม feature ที่สร้าง ณ จุดนี้ อยู่ feature branch ที่เชื่อมและ branch ออกจาก master
Chart นี้ visualize ว่า branches ทำงานอย่างไร (รูปภาพ)
เห็น 2 branches: topic และ master topic branch = feature branch A, B, C = commits ตามเวลา C = latest commit บน feature branch G = latest commit บน master branch
สถานการณ์นี้เกิดหากทีมอื่น commit ไป master branch หลัง code review เนื่องจาก latest master branch ต่างจาก version master branch บน machine (จาก git pull) มี version control issues อาจเกิด merge conflicts
จาก feature branch ใช้ git merge master merge master branch เข้า feature branch จะทำให้ version master บน machine up-to-date และเมื่อโค้ดผ่าน code review ใช้ git push origin master push การเปลี่ยนแปลงไป master branch
อีกครั้ง push ไป master branch ก็ต่อเมื่อผ่าน guidelines ทั้งหมด best practice: อย่า push code ตรงไป master เพราะ bugs/issues จาก untested code
สรุป
การคล่องแคล่วคำสั่ง git เป็นความจำเป็นสำหรับนักพัฒนาทุกคน การรู้ว่าจะใช้คำสั่งไหนเมื่อไหร่ จะเก็บ latest version ของโค้ดไว้และป้องกัน merge issues กับ branches อื่นๆ
ใช้ git ในบริบท contribute codebase บริษัทหรือ open source ต้อง follow guidelines แต่ละ project review code ต่างกัน การ follow guidelines โดยเฉพาะตอน git merge master เป็นสิ่งจำเป็น