ในกรณีที่เราต้องการใช้ code จาก git repository ของคนอื่นมาเป็นต้นแบบ หรือเป็นโจทย์การเขียนโปรแกรม และเราอยากเก็บ versioning การแก้ไข หรือคำตอบ ของเราเองด้วย จะใช้การ fork repository ต้นฉบับ มาเป็น repository ของเราเอง

แต่ปัญหาเกิดขึ้นเมื่อผ่านไป repository ต้นฉบับ มีการอัพเดท เช่น เพิ่มบทเรียน หรือ อัพเกรดเวอร์ชัน แล้วเราจะทำให้ repository ของเราอัพเดทล่าสุดได้อย่างไร โดยที่การแก้ไขของเราไม่หาย

1. ขั้นแรก git clone Fork ของเรามาก่อน (ถ้ายังไม่ได้ Clone)

git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git 

เช่น 

git clone https://github.com/gnoparus/course-v3.git

2. เพิ่ม repository ต้นฉบับ ไปที่ remote upstream

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream

เช่น

cd tutorials/fastai/course-v3
git remote add upstream https://github.com/fastai/course-v3.git
git fetch upstream

3. อัพเดท Fork ของเรา ด้วยการ pull จาก repo ต้นฉบับ

git pull upstream master

4. Push การเปลี่ยนเปลงไปยัง fork repo ของเรา

git push

หมายเหตุ

  • ตัวอย่าง pull จาก master ของ repo ต้นฉบับ แต่จริง ๆ สามารถเลือกได้ว่าจะ pull จากไหน
  • เราสามารถแสดงรายการ remote repo ทั้งหมด ด้วยคำสั่ง git remote -v

เครดิต

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.