คู่มือนี้ออกแบบมาเพื่อให้คุณทำตามแบบ Step-by-Step พร้อมคำอธิบายภาษาไทยและ Data Flow ให้เข้าใจง่ายที่สุดครับ
ก่อนเริ่มคำสั่ง มาเข้าใจก่อนว่าไฟล์เราเดินทางอย่างไร:
[ Working Directory ] --(git add)--> [ Staging Area ] --(git commit)--> [ Local Repo ] --(git push)--> [ Remote (GitHub) ]
(แก้ไขไฟล์) (เตรียมส่ง) (เก็บประวัติ) (สำรองออนไลน์)
เราจะใช้ SSH เพื่อไม่ต้องพิมพ์รหัสผ่านบ่อยๆ และมีความปลอดภัยสูง
# สร้าง key ใหม่ (ตั้งชื่อไฟล์ว่า id_github_workshop เพื่อไม่ให้ทับของเก่า)
ssh-keygen -t ed25519 -C "[email protected]"- เมื่อถามชื่อไฟล์ให้ใส่:
/Users/yourname/.ssh/id_github_tutorial - ก๊อปปี้ Public Key ไปแปะใน GitHub Settings -> SSH Keys:
cat ~/.ssh/id_github_tutorial.pub- ตั้งค่าให้ working dir นี้ใช้ key นี้ในการเชื่อม Github
git config core.sshCommand "ssh -i ~/.ssh/id_github_tutorial"เราจะตั้งค่าเฉพาะโฟลเดอร์นี้เท่านั้น ไม่ไปยุ่งกับ global
git config user.name "Your Name"
git config user.email "[email protected]"
# เช็คความเรียบร้อย
git config --listgit init # เปลี่ยนโฟลเดอร์ปกติให้เป็น Git Repo
git remote add origin [email protected]:username/repo-name.git # เชื่อมต่อกับ GitHub
git branch -M main # ตั้งชื่อ branch หลักเป็น mainecho "# Git Tutorial" > README.md # สร้างไฟล์
git status # เช็คสถานะ (จะเห็นไฟล์เป็นสีแดง)
git add README.md # ย้ายไป Staging (จองคิว)
git commit -m "feat: initial commit with readme" # บันทึกประวัติ (ลงถังในเครื่อง)
git push -u origin main # ดึงขึ้น GitHub ครั้งแรกพร้อมผูกความสัมพันธ์ (-u)แนะนำรูปแบบ: ชื่อเล่น/ประเภทงาน/สิ่งที่ทำ
boom/feat/login-page(ฟีเจอร์ใหม่)elon/fix/bug-css(แก้บัค)mark/chore/update-deps(งานบ้านทั่วไป)
# สร้าง branch ใหม่จากจุดที่เรายืนอยู่
git checkout -b boom/feat/add-content
# ... แก้ไขไฟล์ ...
git add .
git commit -m "feat: add workshop content"
git push origin boom/feat/add-content # ส่งงานขึ้นไปให้เพื่อนดูเมื่อคนอื่น (หรือเราเอง) ทำงานเสร็จแล้ว และต้องการเอามาประจบกันใน main
git checkout main # ย้ายกลับมาที่ main
git pull origin main # ดึงงานใหม่ล่าสุดลงมาก่อน (กันพลาด)
git merge boom/feat/add-content # รวมร่าง!
git push origin main # ส่งงานที่รวมแล้วขึ้น GitHubgit log --oneline --graph --all # ดูสายพานงานว่าใครแตกไปไหน รวมที่ไหนถ้าทำ branch ปัจจุบันพังจนกู่ไม่กลับ:
- ใช้
git logหาเลข Commit ID ที่ยังดีอยู่ (เช่นabc1234) - สร้าง Branch ใหม่จากจุดนั้นเลย:
git checkout -b new-start-branch abc1234
- ลบ Branch ที่พังทิ้งไป:
git branch -D branch-ที่-พังวิธีนี้ปลอดภัยกว่าการพยายามย้อนอดีต (Reset) เพราะประวัติงานจะไม่หายไปไหน
git status: ฉันอยู่ที่ไหน มีอะไรแก้บ้าง?git pull: ดึงงานเพื่อนมาgit add .: เก็บของลงตะกร้าgit commit -m "...": แปะป้ายบันทึกgit push: ส่งของขึ้นหิ้งgit checkout -b ...: เปิดร้านใหม่ (Work on new task)