Skip to content

จัดการรีโมท (git remote)

git remote คือชุดคำสั่งสำหรับจัดการการเชื่อมต่อระหว่างโปรเจกต์ในเครื่องกับ repository ที่อยู่บนออนไลน์ เช่น GitHub, GitLab, Bitbucket

วิธีดูและเพิ่ม remote

  • ดูรายชื่อ remote ที่เชื่อมต่ออยู่:
sh
git remote -v
  • เพิ่ม remote ใหม่ชื่อ origin:
sh
git remote add origin https://github.com/username/project.git
  • เปลี่ยน URL ของ remote:
sh
git remote set-url origin https://github.com/username/newproject.git
  • ลบ remote ที่ไม่ใช้แล้ว:
sh
git remote remove origin

ตัวอย่างสถานการณ์จริง

"สร้างโปรเจกต์ในเครื่องเสร็จ อยากเชื่อมกับ GitHub ก็ใช้ git remote add origin ... จากนั้น push โค้ดขึ้นออนไลน์ได้เลย" "ถ้าย้าย repo ไปที่ใหม่ แค่ set-url ก็เปลี่ยนปลายทางได้ทันที"

เคล็ดลับ

  • ตั้งชื่อ remote ให้สื่อความหมาย เช่น origin (มาตรฐาน), upstream (สำหรับ repo ต้นฉบับ)
  • 1 โปรเจกต์เชื่อมต่อได้หลาย remote (เช่น origin, upstream)
  • ใช้ git remote rename oldname newname เปลี่ยนชื่อ remote ได้

"git remote คือสะพานเชื่อมระหว่างโค้ดในเครื่องกับโลกออนไลน์"