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

Git Pull All Branches: ดึงทุก Branch จาก Remote Repository

Git ให้คุณจัดการ Multiple Separate Lines of Development สำหรับโปรเจกต์ บรรทัดการพัฒนาเหล่านี้เรียกว่า Branches คุณสามารถดึง Latest Version ของ Branch จาก Remote Repository แยกกันได้ หรือดึง Latest Version ของทุก Branches พร้อมกันได้

ในคู่มือนี้ เราจะพูดถึงวิธีใช้ git fetch --all และ git pull --all เพื่อดึงการเปลี่ยนแปลงจาก Remote Repository

Branching คืออะไร?

สมมติเราทำงานบน Blog Website เราจะเพิ่ม Feature ให้ User Comment ได้ เราไม่ต้องการ Feature นี้เป็นส่วนของ Main Version เพราะยังกำลังพัฒนาอยู่

เราสามารถใช้ Git Branch สร้าง Branch ชื่อ "comments" เก็บโค้สำหรับ Feature Commenting นี้ จะให้เราทำงาน Feature นี้โดยไม่เปลี่ยน Main Version ของ Codebase ที่ Deploy บน Website

Branches สามารถเก็บ Local หรือ Remote ได้ หากทำงาน Local Version Branch จะเป็น Local Remote Branches จะเก็บอยู่กับ Main Version ของโปรเจกต์

Git: Fetch All Branches

เราทำงานโปรเจกต์ blog-site มี 2 Branches: origin master และ origin dev

Branch dev มี Experimental Features เราคิดว่าเพื่อนร่วมทีม Push การเปลี่ยนแปลงไปทั้ง 2 Branches เราต้องการตรวจสอบและดึง Metadata ของการเปลี่ยนแปลงหากมี

เราสามารถทำได้ด้วยคำสั่ง Fetch Fetch จะบอก Git ให้ดึง Metadata จาก Remote Branch เกี่ยวกับ Latest Updates Fetch ไม่ได้อัปเดตไฟล์ที่เก็บใน Local Version ของ Repository

เพื่อ Track ทุก Remote Branches และ Fetch Metadata สำหรับ Branches เหล่านั้น เราใช้ git fetch --all:

git fetch --all

คำสั่งคืนค่า:

Fetching origin remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/career-karma-tutorials/blog-site 3fcea0c..da74d68 dev -> origin/dev

Fetch Command ได้ Fetch การเปลี่ยนแปลงทั้งหมดที่เราทำไปที่ Remote Repository Fetch รู้ว่า Remote Dev Branch มีการเปลี่ยนแปลงที่เรายังไม่มีบน Local เราเพิ่งดึง Metadata ของ Commits เหล่านั้นมา

เราสามารถดึง Metadata ของ Individual Branch ได้ด้วย git fetch origin <branch-name>

Git: Pull All Branches

ถ้าต้องการ Update Local Working Copy พร้อมกับดึง Metadata นั่นคือที่มาของ git pull

ตอนนี้เรารู้ว่ามีการเปลี่ยนแปลงที่ Repository เรายินดี Merge การเปลี่ยนแปลงเหล่านี้กับ Local Repository เพื่อดาวน์โหลดการเปลี่ยนแปลงมาที่ Local เราใช้ git pull --all:

git pull --all

เราใช้ Flag --all เพื่อระบุว่าต้องการดึงการเปลี่ยนแปลงจากทุก Branch คำสั่งคืนค่า:

Fetching origin remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/career-karma-tutorials/blog-site 3fcea0c..da74d68 dev -> origin/dev Updating 3fcea0c..da74d68 Fast-forward README.md | 1 + 1 file changed, 1 insertion(+)

Git Pull Command จะรัน git fetch ก่อนเพื่อเช็คการเปลี่ยนแปลง Fetch Operation คืน Metadata ของ Commits ของเรา แล้ว Git Pull จะดึงการเปลี่ยนแปลงทั้งหมดที่เราทำไปที่ Remote Repository และเปลี่ยน Local Files ของเรา

เราเห็นว่า README.md ถูกเปลี่ยนบน Remote Repository ตอนที่เรารัน Pull Operation เราก็มีการเปลี่ยนแปลงบน Local แล้ว

เพื่อดึงโค้ดจาก Branch เดียว สามารถใช้ git pull origin <branch-name>

สรุป

git fetch --all ดึง Metadata ของการเปลี่ยนแปลงทุก Branch ใน Repository git pull --all ดาวน์โหลดการเปลี่ยนแปลงทั้งหมดทั่วทุก Branch มาที่เครื่อง Local

ตอนนี้คุณมีความรู้เพียงพอที่จะ Pull All Branches จาก Git ได้แล้ว!