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

Git Clone Specific Branch: คู่มือ Clone Branch เฉพาะเจาะจง

คำสั่ง git clone --single-branch --branch Clone Branch เฉพาะเจาะจง คำสั่งนี้ให้คุณ Copy เนื้อหา Repository โดยไม่ต้องดาวน์โหลดทุก Branches เหมาะสำหรับ Repository ขนาดใหญ่ที่คุณต้องการโค้ดที่จะใช้งานจริงเท่านั้น

โดย Default คำสั่ง git clone จะ Duplicate ทุก Branches จาก Git Repository ในการ Clone Branch เฉพาะ คุณต้องใช้ Flag --single-branch ร่วมกับคำสั่ง git clone

ในคู่มือนี้ เราจะอธิบายวิธี Clone Specific Branch ด้วย Git ใช้คำสั่ง git clone พร้อมตัวอย่างเพื่อเสริมการเรียนรู้

Cloning คืออะไร?

Cloning ให้คุณ Save Copy ของ Repository ที่โฮสต์ที่อื่นมาไว้ที่เครื่อง Local คุณสามารถ Clone Repository ได้ด้วยคำสั่ง git clone

คำสั่ง git clone หมายความว่า Git Version Control Server ไม่ต้องมี Web Interface คุณสามารถดาวน์โหลด Copy ของ Git Repository จาก Command Line ได้

Clone Specific Branch เป็นวิธีทั่วไปในการลดพื้นที่ Disk ที่ Repository ใช้ เนื่องจากคุณไม่ Clone ทุก Branches ของโปรเจกต์ คุณสามารถดาวน์โหลด Branch อื่นเพิ่มเติมได้ภายหลังหากพบว่าจำเป็น

Git Clone a Specific Branch

คำสั่ง git clone --single-branch --branch Clone Branch เฉพาะจาก Git Repository ระบุชื่อ Branch ที่ต้องการ Clone หลัง Flag --branch คุณสามารถดาวน์โหลด Branches อื่นเพิ่มได้ภายหลังหากต้องการ

คุณสามารถจำกัด Branches ที่ Clone Command ดึงมาได้ด้วย Option --single-branch:

git clone --single-branch --branch <branch-name> <url>

<branch-name> คือที่ระบุชื่อ Branch ที่ต้องการ Clone Branch ที่ Clone ต้อง Exist มิฉะนั้นคำสั่งจะ Error <url> คือ URL ของ Repository ที่ต้องการ Clone Branch

Git Clone a Specific Branch: ตัวอย่าง

เรามี Repository ชื่อ ck-git มี 2 Branches: master และ dev เราต้องการแค่ Master Branch เพราะไม่ได้วางแผนทำงานกับ Dev Branch

เพื่อดึงแค่ Master Branch เราใช้ Option --single-branch กับ git clone:

git clone --single-branch --branch master https://github.com/career-karma-tutorials/ck-git

หลัง Flag --single-branch เราระบุค่าสำหรับ Flag --branch นี่คือที่เราบอก Git ว่า Clone Branch ไหน ต่อมาเราระบุ URL ของ Repository ที่ต้องการ Clone เหมือน git clone ปกติ

ลองดูผลลัพธ์เมื่อรันคำสั่ง:

Cloning into 'ck-git'... remote: Enumerating objects: 37, done. ... Unpacking objects: 100% (37/37), done.

Git Clone Command Copy ck-git Repository มาที่ Local Machine คำสั่ง Clone แค่ Branch "master" เพราะเราใช้ Flag --single-branch

เราสามารถตรวจสอบได้ว่า Clone แค่ "master" Branch โดยเข้า Folder โปรเจกต์ใหม่และรัน git branch:

cd ck-git/ git branch

คำสั่ง git branch แสดง Branches ทั้งหมดที่เก็บ Local ใน Repository:

* master

มีเพียง 1 Branch ถูก Clone คือ Master Branch

คุณต้องระบุ Flag --single-branch หากต้องการ Clone Single Branch Flag --branch เพียงอย่างเดียวระบุ Branch ที่ต้องการ Checkout เมื่อเข้า Repository การ Clone ด้วยแค่ --branch ยัง Fetch ทุก Branches ใน Repository อยู่

Flag --single-branch รองรับตั้งแต่ Git Version 1.7.10 ขึ้นไป

Fetch a Remote Branch

เนื่องจากเราดาวน์โหลดแค่ 1 Branch เราไม่เห็นโค้ดบน Branches อื่นในโปรเจกต์

เราตระหนักว่าต้องการ Copy ของ Branch "dev" ด้วย ไม่เป็นไร เพราะเราสามารถ Fetch Remote Branches ได้หลัง Clone เริ่มต้น เราสามารถดึง Branch "dev" ด้วย git checkout:

git checkout --track origin/dev

คำสั่งนี้จะดึง Dev Branch บน "origin" ของเรา "origin" หมายถึง Remote Repository ที่ Repository ของเราเชื่อมโยงอยู่

Branch "dev" จะถูกบันทึกเป็น Local Branch แล้ว Git HEAD ของเราจะเปลี่ยนไปที่ Branch "dev" หมายความว่าเราจะย้ายจาก Branch ที่อยู่ไปสู่ Branch "dev"

สรุป

คุณสามารถ Clone Specific Branch จาก Git Repository ด้วย git clone --single-branch --branch คำสั่งนี้ดึงไฟล์และ Metadata ทั้งหมดที่เกี่ยวข้องกับ 1 Branch เพื่อดึง Branches อื่น คุณต้อง Fetch เพิ่มภายหลัง

อยากเรียนรู้ Git เพิ่มเติม? ดูคู่มือ How to Learn Git ของเรา จะมีรายการ Online Learning Resources, Courses, และ Books พร้อมคำแนะนำจาก Expert ว่าจะไปจาก Beginner สู่ Expert ได้อย่างไร