Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Git Cherry Pick: คู่มือ Step-by-Step

ในฐานะ Developer มือใหม่ เราเรียน Git ผ่านการทำซ้ำ เราเรียนรู้เร็วว่า git pull, git push, และ git commit แต่ละอย่างหมายถึงอะไร เมื่อเราเปลี่ยนไปทำงานโปรเจกต์ใหญ่และร่วมงานกับ Teams เราเริ่มเรียนรู้ Git Commands ขั้นสูงที่ช่วยให้ Version Control History ของ Codebase ตรงกันระหว่างบุคคลที่ทำงานบนโปรเจกต์

คำสั่งหนึ่งคือ git cherry-pick ใช้เมื่อเราต้องการนำ Commits เฉพาะจาก Branch หนึ่งไป Attach ที่ HEAD ของ Feature Branch อื่นหรือ Master Branch

git cherry-pick คืออะไร?

คิดถึง Cherry-picking เหมือน Group Project แต่ละคนมี Section เฉพาะที่ต้องทำ ท้ายสุดจะรวมแต่ละ Section เป็น Project รวม 1 โปรเจกต์ คุณอาจนำส่วนหนึ่งของ Section ไป Splice เข้ากับส่วนใน Section อีกตัว เพื่อให้ Project ไหลลื่นขึ้น

นี่คือ Git Cherry-pick ในแนวคิด: เรานำ Commit หรือ Multiple Commits จาก Feature Branch หนึ่งไป Attach เป็น Commit ใหม่ที่ Branch อีกตัว มาดูวิธีทำงาน:

Git Cherry Pick: คู่มือ Step-by-Step

Diagram พื้นฐานนี้แสดง 2 Branches จากโปรเจกต์ที่ Git Track ตัวอักษรแทน Commits ต่างๆ ที่ทำไปที่ Git Repository บน Branch แต่ละตัว Dashes แทน History จากเก่าสู่ใหม่ ลองจินตนาการโครงสร้าง 2 Branches และวิธีทำงานตาม Steps:

Steps to complete git cherry-pick

คิดว่าแต่ละ Git Commit หรือตัวอักษร (ใน Diagram) เป็น Cherry แต่ละ Cherry มี Unique Hash ที่ผูกมันไว้ - หากไม่แน่ใจ Hash คืออะไร คิดว่ามันคือ Fingerprint หรือ Unique Identifier ที่ผูกกับ Commit เราจะต้องการ Hash นั้นเพื่อ Pick Cherry นั้นและเพิ่มเข้า Branch อื่น

  1. git checkout <name of branch you’d like to grab commit from>
    Checkout ไป Branch ที่ต้องการ Pick Commit/Cherry จาก
  2. git reflog
    Git Reference Log (reflog) ติดตาม Recent Actions ที่ทำไป ตัวอย่าง git reflog:
    % git reflog
    bf654bb (HEAD -> master, origin/master) HEAD@{0}: commit: last commit message made
    2394353 HEAD@{1}: commit: where head was 2 commit messages ago
    b4b51eb HEAD@{2}: commit: where head was 3 commit messages ago


    Commit Reference Log ของคุณ หากทำงาน Team ใหญ่ อาจมี Info มากกว่านี้ เช่น Date, Time, และ/หรือ Author ที่ทำ Commit ที่นี่เราเห็น Commit Hash, Branch ที่ Commit ทำ, Action ที่ทำ, และ Commit Message จริง

    เมื่อรู้ว่าต้องการ Commit ไหนเพิ่มเข้า Branch อื่น ให้จด Hash ไว้ - Hash คือ String ตัวเลขและตัวอักษรต้นแต่ละบรรทัดในตัวอย่างนี้
  3. git checkout <name of branch you would like to add commit to>
    หลังจด Hash แล้ว Switch ไป Branch ที่ต้องการเพิ่ม Commit เข้าไป
  4. git cherry-pick [-x] <commit hash>
    ใช้ git cherry-pick command พร้อม Commit Hash เพื่อเพิ่ม Commit เข้า Working Tree ของ Branch นั้น ใช้ Flag -x เมื่อ Cherry-pick จาก Public Branch เพราะจะ Append บรรทัด Remark ว่า Cherry-picked จาก Commit เดิม
    ลองดู Diagram อีกครั้งหลัง Cherry-pick:

Git Cherry Pick: คู่มือ Step-by-Step

Cherry (Commit Hash) ที่เราเลือกในตัวอย่างคือ "C" หลังทำตาม Steps ข้างต้น Commit ที่เลือก "C" ในกรณีนี้ ถูกเพิ่มเป็น Commit เข้า History ของ Branch ที่สอง

Final Notes

สิ่งสุดท้ายคือ Copy Notes จาก Cherry-picked Commit ใช้ git notes copy <cherry-picked commit hash> <new-commit-hash> เพื่อ Copy Notes ที่ทำใน Original Commit

อย่าลืม Resolve Merge Conflicts ที่อาจเกิดขึ้นเป็นผลพวง
ในบทความนี้ เราได้ดู Process ที่ใช้ Advanced Git Command ชื่อ git cherry-pick ใช้เมื่อต้องการนำ Commit จาก Branch หนึ่งไปเพิ่มเข้า Branch อื่น