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

Git Pull: คำสั่งดึงโค้ดจาก Remote Repository

คำสั่ง git pull ดึงและดาวน์โหลดเนื้อหาของ repository ไปยัง local machine local repository จะถูกอัปเดตให้แสดงเนื้อหาจาก remote repository

คุณน่าจะเจอสถานการณ์ต้องการ fetch และ download โค้ดจาก Git repository ไป local machine

เช่น ทำงานบนแอปพลิเคชันติดตามออเดอร์ร้านค้า ขณะแก้ไขแอป ต้อง pull code จาก remote repository เพื่อให้ทำงานกับโค้ดล่าสุดเสมอ

นั่นคือที่มาของ git pull ใช้ retrieve content จาก remote repository แล้ว save ไป local machine บทช่วยสอนนี้อธิบายพื้นฐาน pulling code และการใช้ git pull พร้อมตัวอย่าง

Git Pull Command คืออะไร?

"Pulling code" หมายถึงการ download เนื้อหาจาก remote repository และ save ไป computer

ใน Git ใช้ git pull คำสั่งช่วยเหลือ execute 2 คำสั่งอื่น: git fetch และ git merge

วิธีทำงานของ git pull:

1. เมื่อรัน git pull remote repository ที่ pull จะถูก download copy ของโค้ดและ Git commits จะ save ไป machine

2. ตามด้วย Git merge operation merge โค้ดบน local machine กับโค้ดที่ดึงมาใหม่ สร้าง final version ของ codebase เท่ากับ remote branch ที่ดึงมา

คำสั่ง Git Pull

git pull retrieve remote repository และ download โค้ดไป local version

git pull <remote>

พารามิเตอร์ "remote" คือ remote repository ที่ต้องการ pull ไป local machine รันคำสั่ง remote repository จะ retrieve แล้ว merge เข้า local copy

git pull ไม่ affect untracked files จะได้แค่การเปลี่ยนแปลงบน remote branches ที่ Git track การเปลี่ยนแปลงจะ save ไป local working tree

git pull origin

สมมติ pull contents ของ "origin" repository (default remote) ด้วย pull origin master branch:

git pull origin/master

ผลลัพธ์:

From https://github.com/jamesgallagher432/demo-repository
b53b22d..a7d8dc2 master -> origin/master
Updating b53b22d..a7d8dc2
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)

คำสั่ง retrieve remote repository แล้ว merge การเปลี่ยนแปลงเข้า local copy README.md บน remote ต่างจาก local ตอนรัน git pull การเปลี่ยนแปลงถูกระบุและ merge เข้า local machine

ตอนนี้ local machine เก็บโค้ดเดียวกับ remote repository เมื่อทำการเปลี่ยนแปลง จะ compatible กับ latest version เมื่อมี commits ใหม่ push ไป remote ใช้ git pull อีกครั้งได้ version ใหม่

ตรวจสอบไฟล์อัปเดตด้วย git log แสดง commits ทั้งหมดของ repository

Git Pull Remote Branch จาก Repository

หากอยาก pull code จาก branch เฉพาะไป local machine ทำงานบน branch "dev-v0.9" อยาก pull แค่ branch นั้น

ใช้ git checkout ตามด้วย git pull ไวยากรณ์:

git checkout <branch name>
git pull <remote>

ตัวอย่างรัน:

git checkout dev-v0.9
git pull origin

git checkout ย้ายไป branch "dev-v0.9" git pull origin สร้าง copy ของโค้ดบน branch นั้น retrieve และ save หลัง download จะ execute git merge operation อัปเดต local repository ให้เหมือน remote

Git Force Pull

สามารถ force pull operation ได้ มัก force pull เมื่อเจอ untracked file error ที่อยาก discard

ก่อน force pull ต้องแน่ใจว่ายอมรับการสูญเสีย local changes เพราะ force pull จะ overwrite local changes ทั้งหมด

Force pull ขั้นตอน:

  1. Fetch metadata สำหรับ commit ที่ต้องการ save ไป local:
    git fetch --all
  2. Backup current branch:
    git branch -b backup
    backup ทำให้ไม่สูญเสีย changes ที่อยากกลับมาใช้ภายหลัง
  3. Pull changes ด้วย reset command:
    git reset --hard origin/master
    แทน origin/master ด้วย origin และ branch names ของโปรเจกต์ origin = origin repository master = branch name ที่ fetch

อ่านเพิ่มเติมใน git force pull guide

Git Pull vs. Git Fetch

ทั้ง git fetch และ git pull retrieve เนื้อหา remote repository บาง developer สับสนความต่าง

git fetch retrieve แค่ metadata จาก remote repository metadata ใช้ check ว่ามี changes ที่ pull ไป local ได้หรือไม่ git pull download ทั้ง metadata และไฟล์ที่เปลี่ยนแปลง

git pull execute fetch operation ก่อน หากระบุ changes ระหว่าง local และ remote จะ download changes

ใช้ git pull ควรแน่ใจว่าพร้อมรับ changes มิฉะนั้น repo อาจเจอ conflicts เมื่อ commit

สรุป: ใช้ git fetch check changes ระหว่าง local และ remote ใช้ git pull หากต้องการ retrieve metadata และ download changes

สรุป

คำสั่ง git pull fetch และ download โค้ดจาก remote Git repository git pull คล้าย git fetch git fetch ดึงแค่ metadata git pull ดึง metadata และการเปลี่ยนแปลงไฟล์

บทช่วยสอนสำรวจพื้นฐาน pulling code และการใช้ git pull ตอนนี้คุณพร้อมใช้ git pull อย่างมืออาชีพแล้ว!