Git & GitHub

Git คือระบบควบคุมเวอร์ชันแบบกระจาย เก็บประวัติทุกการแก้ไข — แทนที่จะตั้งชื่อไฟล์ final_v1, final_v2 ไปเรื่อย ๆ

Local vs Remote Repository

Local RepositoryRemote Repository
อยู่ที่ไหนโฟลเดอร์โปรเจกต์ในเครื่องเรา (มี .git)เซิร์ฟเวอร์ เช่น GitHub, GitLab
ใช้ทำอะไรบันทึกประวัติ (commit) ทำงานได้แม้ออฟไลน์สำรองข้อมูลและทำงานร่วมกันเป็นทีม
คำสั่งหลักgit add, git commitgit push, git pull

เส้นทางของไฟล์ 4 ที่

Working Tree ไฟล์ที่กำลังแก้ git add Staging Area เตรียมจะ commit git commit Local Repo ประวัติในเครื่อง git push git pull Remote GitHub git add ยังไม่เข้า repository — แค่เข้า staging area เท่านั้น ตรวจสถานะระหว่างทางด้วย git status

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 ตัดสินใจแทนไม่ได้ว่าจะเอาของใคร
A และ B pull A push สำเร็จ B push ไม่ได้ — remote ใหม่กว่า B ต้อง pull / fetch ก่อน Merge Conflict B แก้เอง แล้ว add + commit + push

Git ไม่ตัดสินใจแทนเรา — ต้องเลือกหรือรวมโค้ดด้วยตัวเองเสมอ

git add <resolved-files>
git commit -m "Resolved conflicts"
git push

เครื่องมือหลังติดตั้ง Git

ดาวน์โหลด Git ได้ที่ git-scm.com/downloads ส่วน remote repository ใช้ github.com