การย้าย Git Repository ไปยัง Git Remote server ตัวอื่นๆ
มีเหตุให้ต้องย้าย Remote Repository ของ Gitlab เพราะเกินโควต้า 5 คนฟรี ต้องจ่ายตังค์ จึงตั้ง server gitlab hosting ไว้ใช้กับทีมซักหน่อย
ขั้นตอนนี้ผมสมมุติว่าทุกคนย้าย code ไปยัง remote server ใหม่เรียบร้อยเหลือแค่ขั้นตอนการชี้ไปยัง remote server ใหม่ใน repo เดิมของเรา
พระเอกของเราวันนี้คือ Remote Repository เป็น git hosting ที่ทำให้เราสามารถเก็บ source code ของเราไว้บน server ได้ ซึ่งปัจจุบันมีให้บริการหลายเจ้าเช่น gitlab, github, bitbucket ซึ่งปกติเราสามารถเพิ่ม remote repository ได้หลายเจ้าไว้ใน project เดียวได้ มาดูคำสั่งที่เอาไว้จักการ Remote Repository กัน
Git จะมีคำสั่ง
git remote ไว้ค่อยจัดการ
Remote Repository
หลักๆ git remote จะมีคำสั่งที่ใช้บ่อยๆ ดังนี้
- git remote -v เป็นคำสั่ง list และตรวจสอบรายการ remote ที่เราใช้
- git remote add <name> <url> เป็นคำสั่งเพิ่ม remote repository ให้กับ repo
- git remote rm <name> เป็นคำสั่งลบ remote repository
ตรวจสอบ Git Remote
ขั้นตอนแรกให้เราไปที่ git repository เดิมของเราแล้วตรวจสอบ git remote ด้วยคำสั่ง `git remote -v` ตัวอย่างนี้เป็นของ gitlab จะเห็นรายการ git remote ที่เราเพิ่มไว้ชื่อ remote ว่า origin ซึ่งเป็นชื่อ default
กรณี clone with ssh (กรณีนี้เราต้อง add ssh key เครืองของเราให้ remote repository ก่อนถึงจะใช้ได้ ข้อดีไม่ต้องกรอก username & pass)
git remote -v
origin git@gitlab.com:khonkaenhospital/test.git (fetch)
origin git@gitlab.com:khonkaenhospital/test.git (push)
กรณี clone with https
git remote -v
origin https://gitlab.com/khonkaenhospital/test.git (fetch)
origin https://gitlab.com/khonkaenhospital/test.git (push)
เมื่อเจอแล้วเราจะเห็น url ประมาณนี้และจะมีชื่อ remote กำกับว่าเป็น origin ซึ่งชื่อนี้เราสามารถตั้งเองได้ตอนที่ใช้คำสั้ง git remote add <name> <url>
Add Remote ใหม่ เข้า git repository
เราสามารถ add url remote ใหม่เข้าไปเพิ่มหลายๆ ตัวได้เพียงแค่ตั้งชื่อไม่ให้ซ้ำกัน
url ก็ไปดูที่ remote repository ของแต่ละค่ายเอง
git remote add <name> <url>
ตัวอย่าง
git remote add github https://github.com/khonkaenhospital/test.git
add เสร็จก็ใช้ คำสั่ง git remote -v เพื่อดูรายการที่เพิ่มเข้าไป ก็จะเห็นรายการที่เราได้ add เพิ่มเข้าไป
git remote -v
github https://github.com/khonkaenhospital/test.git (fetch)
github https://github.com/khonkaenhospital/test.git (push)
origin git@gitlab.com:khonkaenhospital/ihospital-api.git (fetch)
origin git@gitlab.com:khonkaenhospital/ihospital-api.git (push)
ตอนที่จะ pull เราก็แค่เลือก remote เช่น git pull <remote name> <branch>
git pull github develop // ไปที่ดึงที่ github
git pull origin develop // ไปดึงที่ gitlab
กรณีนี้จะใช้ก็ต่อเมื่อเราต้องการ pull หรือ push ไปหลายๆ remote แต่ตามตัวอย่างนี้ผมจะใช้แค่ remote เดียวเพื่อไม่ให้สับสนตอนใช้งาน
ลบ Remote Repository
กรณีที่เราอยากใช้แค่ remote เดียวเราก็แค่ลบรายการ remote เดิมออกไป
การลบ remote จะไม่เกี่ยวข้องกับ code เป็นแค่การลบ url ที่จะชี้ว่าเราจะเอา git repository นี้ไปเก็บที่ไหน
คำสั่ง git remote rm <remote name>
git remote rm oriign
เราจะลบ remote เดิมที่ชื่อ origin ออกและจะ add เข้าไปใหม่ด้วยชื่อเดิมแต่เป็น remote repository ใหม่ ปกติจะดูได้ที่ remote repository บนเว็บนั้นๆ ตัวอย่างนี้ใช้ gitlab ซึ่งเราต้องไป copy มาแล้วเลือกว่าจะใช้ตัวไหน ปกติก็จะเป็นแบบ git clone with https
เมื่อได้มาแล้วก็ลองใช้คำสั่ง git remote add ได้เลย
git remote add origin https://github.com/khonkaenhospital/test.git
ลองใช้คำสั่งเพื่อดูรายการ remote ที่ add เข้าไป
git remote -v
origin https://github.com/khonkaenhospital/test.git (fetch)
origin https://github.com/khonkaenhospital/test.git (push)
ตอนใช้งานก็แค่
git pull origin develop
git push origin develop
สรุปคำสั่ง
git remote -v
origin https://github.com/khonkaenhospital/test.git (fetch)
origin https://github.com/khonkaenhospital/test.git (push)
git remote rm orgin
git remote add origin https://github.com/khonkaenhospital/test.git
git remote -v
origin https://github.com/khonkaenhospital/test.git (fetch)
origin https://github.com/khonkaenhospital/test.git (push)