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

Git List Remote Branches: แสดง Branch ทั้งหมดบน Remote

คุณสามารถ List Remote Branches ที่เชื่อมโยงกับ Repository ได้ด้วย git branch -r, git branch -a หรือ git remote show หากต้องการดู Local Branches ใช้ git branch

คำสั่ง git branch ให้คุณเห็น List ของ Branches ทั้งหมดที่เก็บใน Local Version ของ Repository เพื่อดู Remote Branches ที่เชื่อมโยงกับ Repository ของคุณ ต้อง Append Flag -r ไปท้ายคำสั่ง git branch

ในคู่มือนี้ เราจะอภิปรายวิธีใช้ git branch -r แสดง Remote Branches และวิธีใช้ git remote show แสดง Branches บน Remote Version ของ Repo

Git: List Remote Branches

มี 3 วิธี List Remote Branches ที่เชื่อมโยงกับ Git Repository:

  • git branch -a: ดู Local และ Remote Branches ทั้งคู่
  • git branch -r: ดูเฉพาะ Remote Branches
  • git remote show: ดู Remote Branches และ Metadata ที่เกี่ยวข้อง

คำสั่งที่ใช้บ่อยที่สุดคือ git branch -a และ git branch -r เพราะ List แค่ Branches git remote show ให้ข้อมูลรายละเอียดมากกว่าที่ไม่ได้จำเป็นตลอด

Git: List All Remote Branches Using git branch

เรามี Git Repository ชื่อ ck-git เราไม่แน่ใจว่า Branch ที่ต้องการสร้าง dev2.2-fix มีอยู่ใน Repository หรือไม่

Flag git branch -r

เพื่อเช็ค Branch นี้ เราใช้คำสั่ง git branch:

git branch

คำสั่งคืน List ของ Local Repository Branches ทั้งหมด:

* master dev

Asterisk (*) หมายถึง Branch ที่เรากำลัง View อยู่ เราเห็นว่า Branch ที่ต้องการสร้าง ("dev2.2-fix") ไม่มีอยู่

ก่อนสร้าง Branch เราต้องการเช็คว่ามี Branch นี้บน Remote หรือไม่ ทำได้โดยเพิ่ม Flag -r ให้ git branch:

git branch -r

คำสั่งนี้ดึง Branches บน Remote Version ของ Repository ของเรา:

origin/HEAD -> origin/master origin/dev2.2-fix origin/master

เราเห็นว่า Remote Repository มี Branch ชื่อ dev2.2-fix อยู่แล้ว เรารู้แล้วว่า Branch นี้มีบน Remote แต่ไม่มีบน Local

นี่หมายความว่าเราต้อง Fetch Existing Branch มาที่ Local Machine เพื่อเริ่มเขียนโค้ด เราไม่ต้องสร้าง Branch ใหม่

เราสามารถ Fetch Existing Branch จาก Remote Repository ด้วย Git Fetch Command:

git fetch origin dev2.2-fix

นี่จะดึง Branch dev2.2-fix จาก Origin Repository ของเรา "origin" คือชื่อของ Main Remote Repo ที่เรา Push โค้ดไป เราเห็นว่าหลังรันคำสั่ง Branch ใหม่ถูกสร้าง:

From https://github.com/career-karma-tutorials/ck-git * branch dev2.2-fix -> FETCH_HEAD

Flag git branch -a

Flag -a ที่เชื่อมโยงกับ git branch คืน Local และ Remote Branches ทั้งหมดที่เชื่อมโยงกับ Repository

พิจารณาคำสั่ง:

git branch -a

คำสั่งคืน:

* master remotes/origin/activity-feed remotes/origin/master

เราเห็น Branches ที่ไม่ปรากฏตอนรัน git branch -r เพราะ git branch -r คืนแค่ Remote Branches git branch -a คืน Remote Tracking Branches และ Local Branches

Remote Branches ถูกระบุด้วย Label "remotes"

Git: List All Remote Branches Using git remote show

คำสั่ง git remote show แสดงข้อมูลรายละเอียดเกี่ยวกับ Branches ที่เชื่อมโยงกับ Remote Repository คำสั่งนี้รับ 1 Argument: ชื่อ Remote ที่ต้องการ View Branches

คำสั่ง git branch -r เพียงพอหากต้องการ Overview สั้นๆ ของ Branches ทั้งหมดบน Remote หากต้องการข้อมูลรายละเอียดมากขึ้น git remote show อาจมีประโยชน์มากกว่า คำสั่งนี้คืน:

  • Remote Branches ทั้งหมด
  • Local Branches ที่ Config กับ git pull
  • Branches ที่ Config กับ git push

ลองรัน git remote show บน Remote "origin" ของเรา ซึ่งเป็นชื่อ Main Remote ที่เชื่อมโยงกับโปรเจกต์ เราคาดหวังว่าจะเห็น Origin Master Branch, Main Branch บน Remote และ Branches อื่นๆ ที่เรามี

สำหรับ User ส่วนใหญ่ คำสั่งนี้ให้ข้อมูลมากกว่าที่ต้องการ แต่มีไว้หากต้องใช้

ลองดึง List ทุก Branches บน Remote Repository ด้วย git remote show:

git remote show origin

คำสั่งแสดง Remotes ทั้งหมดที่เชื่อมโยงกับ "origin" นี่คือ Main Remote ที่ Attached กับ Repo ของเรา ลองดูว่าคำสั่งแสดงอะไร:

* remote origin Fetch URL: https://github.com/career-karma-tutorials/ck-git Push URL: https://github.com/career-karma-tutorials/ck-git HEAD branch: master Remote branches: dev2.2-fix tracked master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (local out of date)

เราเห็นว่ามี 2 Branches บน Remote Repository ที่เรา Track Branches นี้คือ master และ dev2.2-fix

เรายังไม่ได้ Config Pull หรือ Push Operation กับ Branch dev2.2-fix เพราะเรายังไม่ Pull Code จากหรือ Push Code ไป Branch นั้น

สรุป

คำสั่ง git branch -r ให้ List ทุก Branches บน Remote เฉพาะ หากต้องการข้อมูล Remote มากขึ้น ใช้ git remote show

ตอนนี้คุณมีความรู้เพียงพอที่จะใช้ Git List Branches on Remote Commands แล้ว เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ Git อ่านคู่มือ How to Learn Git ของเรา