ผู้เริ่มต้นใช้ Git มักสบัสนกับ git push, git pull, และ git fetch เพราะดูเหมือนกันแต่ทำอะไรต่างกัน บทความนี้จะอธิบายความแต่งต่างอย่างชัดเจนเพื่อให้คุณใช้ commands ที่ถูกต้อง
git push, git pull, git fetch ต่างกันอย่างไร
git fetch – ดาวน์โหลด commits, branches, และ tags จาก remote repository มาให้ local machine แต่ไม่ merge กับ local branches
$ git fetch origin
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:user/repo
e1c3d8d..6e0373f main -> origin/main
git pull – รวม git fetch และ git merge เข้าด้วยกัน ดาวน์โหลด commits มาแล้ว merge เข้ากับ local branch ทันที
$ git pull origin main
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:user/repo
e1c3d8d..6e0373f main -> origin/main
Updating e1c3d8d..6e0373f
Fast-forward
file.txt | 2 ++
1 file changed, 2 insertions(+)
git push – อัปโหลด commits จาก local repository ไปยัง remote repository
$ git push origin main
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:user/repo.git
6e0373f..a1b2c3d main -> main
ตารางเปรียบเทียบ
| Command | ทำอะไร | ทิศทาง | เมื่อใช้ |
| git fetch | ดาวน์โหลด updates | Remote → Local | ดูการเปลี่ยนแปลงจาก remote ไม่แก้ไข |
| git pull | ดาวน์โหลด + merge | Remote → Local | นำการเปลี่ยนแปลงเข้ามาใช้ใน local branch |
| git push | อัปโหลด commits | Local → Remote | ส่ง work ของเราไปให้ remote repository |
