Git & GitHub
Git คือระบบควบคุมเวอร์ชันแบบกระจาย เก็บประวัติทุกการแก้ไข — แทนที่จะตั้งชื่อไฟล์ final_v1, final_v2 ไปเรื่อย ๆ
Local vs Remote Repository
| Local Repository | Remote Repository | |
|---|---|---|
| อยู่ที่ไหน | โฟลเดอร์โปรเจกต์ในเครื่องเรา (มี .git) | เซิร์ฟเวอร์ เช่น GitHub, GitLab |
| ใช้ทำอะไร | บันทึกประวัติ (commit) ทำงานได้แม้ออฟไลน์ | สำรองข้อมูลและทำงานร่วมกันเป็นทีม |
| คำสั่งหลัก | git add, git commit | git push, git pull |
เส้นทางของไฟล์ 4 ที่
Working Tree → (add) → Staging Area → (commit) → Local Repo → (push) → Remote
คำสั่งที่ต้องจำ
| คำสั่ง | ทำอะไร |
|---|---|
git --version | เช็คเวอร์ชัน Git |
git init | ตั้งค่าให้โฟลเดอร์นี้ใช้ version control ได้ |
git add main.html | เพิ่มไฟล์ที่ระบุเข้า staging area |
git add . | เพิ่มทุกไฟล์เข้า staging area |
git add *.html | เพิ่มทุกไฟล์นามสกุล .html |
git status | ดูสถานะ — ไฟล์ไหนแก้แล้ว ไฟล์ไหนยังไม่ add ไฟล์ไหนอยู่ใน staging |
git commit -m "ข้อความ" | บันทึกเข้า local repository |
git reset --option <commit_id> | ย้อนสถานะของ Git |
คำสั่งฝั่ง remote
git remote -v # ดู remote ที่ผูกไว้ git remote add origin <repository-url> # ผูก remote repository git push # ส่งขึ้น GitHub git pull # ดึงของใหม่ลงเครื่อง git clone <repository-url> # คัดลอกโปรเจกต์ทั้งก้อนลงมา
Merge Conflict
เกิดเมื่อ: ผู้ใช้หลายคนแก้ไขส่วนเดียวกันของไฟล์เดียวกัน แล้ว Git ตัดสินใจแทนไม่ได้ว่าจะเอาของใคร
Git ไม่ตัดสินใจแทนเรา — ต้องเลือกหรือรวมโค้ดด้วยตัวเองเสมอ
git add <resolved-files> git commit -m "Resolved conflicts" git push
เครื่องมือหลังติดตั้ง Git
- Git Bash — MinGW (Minimalist GNU for Windows) ใช้คำสั่งแบบ Unix
- Git CMD — command prompt ของ Windows
- Git GUI — หน้าจอกราฟิกของ Git
- GitHub Desktop / VS Code — จัดการ commit และ push ผ่าน GUI ได้เลย
ดาวน์โหลด Git ได้ที่
git-scm.com/downloads ส่วน remote repository ใช้ github.com