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

Git Delete Branch: คู่มือลบ Branch Local และ Remote

การลบ Git Branches เป็น Common Practice หลังจาก Merge Branch เข้า Codebase แล้ว คุณสามารถลบ Git Branch บนเครื่อง Local ด้วย Flag git branch -d คำสั่ง git push origin --delete ลบ Branch จาก Remote Repository

Branching ให้คุณสร้าง Independent Versions ของโปรเจกต์ที่ Edit ได้โดยไม่กระทบ Main Version เมื่อเสร็จ Branch คุณควรลบมัน จะช่วย Keep Codebase Clean

มี 2 Approaches สำหรับลบ Branch ใน Git วิไหนใช้จะขึ้นอยู่กับว่า Branch ที่ลบอยู่ Local Machine หรือ Remote Repository

บทช่วยสอนนี้อภิปรายพร้อมตัวอย่างวิธีลบ Remote และ Local Branches ใน Git จบบทความคุณจะเป็น Expert ในการลบ Local และ Remote Branches

Git Branching

Branching เป็น Essential Aspect ของ Version Control Systems เช่น Git ใน Git Branches ให้คุณสร้าง New Version ของ Existing Project คุณทำ Changes ไปที่ New Branch โดยไม่กระทบ Original Version ของ Project

เช่น คุณอาจสร้าง Branch ทำงานเพิ่ม Feature ไปที่ Project อาจสร้าง Branch อีกตัวเก็บ Code สำหรับ Bug Fix ที่กำลังทำงาน

Branching ให้คุณทำ Changes ไป Codebase โดยไม่เปลี่ยน Main Version ของ Code จนกว่าจะพร้อม หากสนใจ Git Branches อ่าน Beginner's Guide to git branch command ของเรา

Delete Branch in Git

คุณอาจตัดสินใจลบ Branch หลายเหตุผล บางทีเสร็จงาน Branch และ Integrated Changes ที่ทำเข้า Main Version ของโปรเจกต์ แล้วไม่ต้องการ Branch อีกต่อไป

Git Delete Local Branch

คุณสามารถลบ Git Branch จาก Local Machine ด้วย git branch -d command Flag -d หมายถึงต้องการ Delete Branch

สมมติเรามี Local Branch ชื่อ fix-issue49 ที่ Merge กับ Main Version ของโปรเจกต์เมื่อเร็วๆ นี้ Branch นี้มี Bug Fix ที่เราทำงานอยู่ เนื่องจากไม่ต้องการ Local Branch นี้แล้ว เราพร้อมลบมัน

ไม่สามารถลบ Branch ที่คุณกำลัง View อยู่ ก่อน Delete Local Branch คุณต้อง Navigate ไป Branch อื่นที่ไม่ใช่ Branch ที่ต้องการลบ

เพราะเราต้องการลบ fix-issue49 เราต้อง Navigate ไป Branch อื่นก่อน ทำได้ด้วย git checkout command

คำสั่งด้านล่างให้เรา Navigate ไป Master Branch ใน Local Repository:

git checkout master

ตอนที่เราอยู่ Master Branch แล้ว เราสามารถลบ Local fix-issue49 Branch ได้ ทำได้ด้วยโค้ด:

git branch -d fix-issue49

Flag -d บ่งชี้ว่าเราต้องการ Delete Branch fix-issue49 คือชื่อ Branch ที่ต้องการลบ เวลารันคำสั่งนี้ Git ลบ Local Branch fix-issue49

หาก Git เจอปัญหาในการ Delete Branch การ Delete Operation จะหยุด

คุณสามารถใช้ Flag -D (ตัวใหญ่) Force Delete Local Branch Flag -D จะลบ Branch ไม่ว่า Merge ไป Branch อื่นใน Codebase หรือไม่

ใช้ Flag -D ด้วยความระมัดระวัง เพราะ Flag ลบ Branch ทันที เว้นแต่คุณมั่นใจเต็มที่ว่าต้องการลบ Branch ควรใช้ Flag -d

Git Delete Remote Branch

เพื่อลบ Remote Branch ใน Git คุณสามารถใช้คำสั่ง นี้คือคำสั่งที่สั่ง Git Push Local Changes ไป Remote Repository ใน Process นี้ Git ลบ Branch ที่ระบุว่าต้องการลบ

สมมติเราต้องการลบ Branch ชื่อ fix-issue12 Branch นี้เก็บใน Remote Repository Remote origin ของเราอ้างถึง Remote Repository ของเรา เราสามารถลบ Branch fix-issue12 ได้ด้วยคำสั่ง:

git push origin --delete fix-issue12

คำสั่งด้านบนลบ Remote fix-issue12 Branch

หลังรันคำสั่งนี้ เราควรรัน Fetch Command เพื่อดึง Copy Up-to-date ของทุก Branches ที่เก็บบน Remote Repository นี่จะให้เราเห็นบน Local Machine การเปลี่ยนแปลงที่ทำไปที่ Remote Repository

คำสั่งด้านล่างคือสิ่งที่เราพิมพ์เข้า Program เพื่อ Fetch Branches บน Remote Repository:

git fetch -p

เวลารันคำสั่งนี้ Local Git Repository จะ Fetch Copy ของ Remote Repository และ Branches ของมัน Flag -p สั่ง Git ลบ Local Branches ที่ไม่มีอยู่บน Remote Repository แล้ว

เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ git fetch command อ่านคู่มือ git fetch ของเรา

สรุป

Developers มักลบ Branches หลัง Merge กับ Others ใน Repository

คำสั่ง git branch -d ให้คุณลบ Local Branch คำสั่ง git push origin --delete ให้คุณลบ Remote Branch

ในบทช่วยสอนนี้ เราอภิปรายวิธีใช้ 2 คำสั่งนี้ลบ Branches ใน Git ตอนนี้คุณพร้อมลบ Branches อย่าง Git Pro แล้ว!