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

อัปเดต Git Remote URL ได้ง่าย: คู่มือด่วน

คุณสามารถเปลี่ยน Git Remote URL ได้ด้วยคำสั่ง git remote set-url ไปที่ Repository ที่ต้องการเปลี่ยน Remote URL แล้วรันคำสั่งนี้ คำสั่ง set-url รับสอง Argument: Remote Name และ New Repository URL

คุณเปลี่ยนชื่อ Remote Git Repository ไปไหม? หรือย้าย Remote Repository ไป Location อื่น? ทั้งสอง Operation จะเปลี่ยน URL ของ Git Repository ทำให้ References ไป Remote Repository ของคุณพัง

อย่ากังวล! คำสั่ง git remote set-url มาช่วย คำสั่งนี้เปลี่ยน URL ของ Remote Repository ได้

ในคู่มือนี้จะพูดถึง Git Remotes คืออะไร และวิธีเปลี่ยน Git Remote พร้อม Example ช่วยเริ่มต้น

Git Remote คืออะไร?

Git Remote คือ Pointer ที่ Link Local Version ของ Repository ไป Remote Repository

Git เป็น Distributed Version Control System หมายถึง Multiple Developers เก็บ Copies ของ Project บน Machines ของตัวเองได้ Changes ที่คุณทำใน Repository จะเข้าถึงได้โดย Developers อื่นๆ ก็ต่อเมื่อ Push ไป Remote Server

Git Repository หนึ่งสามารถมี Multiple Remotes Linked ได้ แต่Repositories ส่วนใหญ่มี Remote เดียว Repositories ที่มี Multiple Remotes มัก Link ไป Development Environments ต่างๆ เช่น Testing, Staging, หรือ Production

เมื่อเปลี่ยนชื่อ Repository หรือย้ายไป Hosting Platform อื่น ต้อง Update Remote URLs

คำสั่ง git remote set-url เปลี่ยน Git Remote ที่ Associated กับ Repository คำสั่งรับ Remote Name (มักเป็น "origin") และ New Remote URL ที่ต้องการให้ Repository Point ไป

เริ่มด้วยการ Navigate เข้า Repository:

cd Projects/git-submodule-tutorial

ตอนนี้อยู่ใน Git Repository แล้ว เริ่มเปลี่ยน Remotes เช็ค Existing Remotes ด้วย git remote -v:

git remote -v

คำสั่งคืนค่า:

origin https://github.com/Career-Karma-Tutorials/git-submodule-tutorial (fetch)

origin https://github.com/Career-Karma-Tutorials/git-submodule-tutorial (push)

เรามี Remote หนึ่งชื่อ "origin" ใช้ Fetch และ Push Code ไป Remote Repository คุณควรเห็น Output คล้ายกัน เว้นแต่มี Multiple Remotes Set สำหรับ Project

เราจะเปลี่ยน Remote ของ Repository นี้เป็น git-submodule เพราะเราเปลี่ยนชื่อ Repository บน Github ใช้คำสั่ง git remote set-url:

git remote set-url origin https://github.com/Career-Karma-Tutorials/git-submodule

"origin" หมายถึง Remote Name ที่ต้องการเปลี่ยน URL URL ที่ระบุคือ New URL สำหรับ Project

คุณระบุ HTTP หรือ SSH URL เป็น Remote ได้ ตัวอย่าง เปลี่ยน Link เป็น SSH URL แบบนี้:

git remote set-url origin git@github.com:Career-Karma-Tutorials/git-submodule.git

นี่จะ Point "origin" Remote ไป SSH URL

Verify New Remote URL ด้วย git remote -v command:

git remote -v origin

Remotes ของเราเปลี่ยนแล้ว:

origin git@github.com:Career-Karma-Tutorials/git-submodule.git (fetch)

อัปเดต Git Remote URL ได้ง่าย: คู่มือด่วน

"Career Karma เข้ามาในชีวิตฉันเมื่อฉันต้องการมากที่สุด และช่วยจับคู่กับบูทแคมป์ได้เร็ว สองเดือนหลังจบ ฉันพบงานฝันที่ตรงกับคุณค่าและเป้าหมายในชีวิต!"

Venus, วิศวกรซอฟต์แวร์ที่ Rockbot

ค้นหาบูทแคมป์ของคุณ

origin git@github.com:Career-Karma-Tutorials/git-submodule.git (push)

เราทำได้แล้ว!

เปลี่ยน Remote แบบ Manual

คุณเปลี่ยน Remote แบบ Manual ได้โดย Modify Git Repository's Config File ภายใน Working Directory Approach นี้ Practical หากจะทำ Multiple Changes ไป Configuration ของ Git Repository

เปิดไฟล์ .git/config ใน Git Repository ของคุณ Scroll ลงจนถึงบรรทัด [remote "origin"]:

[remote "origin"]
url = git@github.com:Career-Karma-Tutorials/git-submodule.git
fetch = +refs/heads/*:refs/remotes/origin/*

เราสามารถเปลี่ยน Code นี้ Modify "origin" Remote ได้ หลังทำ Changes ที่ต้องการ Save File

ดีกว่าเปลี่ยน Remote ด้วย Git Command เพราะมี Risk สูงกว่าที่จะ Mistake ใน Config File หาก Alter แบบ Manual

fatal: No such remote '[name]'

อาจเจอ Error fatal: No such remote '[name]' ตอนพยายามเปลี่ยน Remote ของ Repository:

fatal: No such remote '[name]'

Error นี้เกิดเมื่อพยายามเปลี่ยน URL ของ Remote ที่ไม่มีอยู่ จะแก้ได้โดย Ensure ว่าพิมพ์ Remote Name ที่ต้องการเปลี่ยน URL ให้ถูกต้อง

สรุป

คุณเปลี่ยน Git Repository's Remote URL ได้ด้วยคำสั่ง git remote set-url หรือ Modify .git/config File ใน Repository

ตอนนี้คุณพร้อมเปลี่ยน Remotes ด้วย Git อย่างมืออาชีพแล้ว!

อ่านเพิ่มเติมเกี่ยว Git ใน Complete How to Learn Git Guide