🌿 Git + GitHub Setup
Git = "time machine สำหรับ code" — เซฟทุกการแก้ · ย้อนได้ทุกจุด · GitHub = "Google Drive สำหรับ code + portfolio ให้คนเห็น" · W04 จะเริ่มใช้เลย · Final Project (W15) ต้องส่งผ่าน GitHub
ทำไม Git ตั้งแต่ W04
Git ดูยากตอนแรก · แต่ถ้าฝึกตั้งแต่ "โปรเจกต์เล็ก" ของ W04 — พอถึง W15 ใช้คล่อง ·
ถ้าเริ่มตอน W15 ที่ project ใหญ่แล้วเรียน Git → สับสนสองชั้น
📥 ขั้นที่ 1 — ติดตั้ง Git
Windows
- ดาวน์โหลด git-scm.com/download/win — โหลดอัตโนมัติ Git for Windows installer
- รัน installer · ค่า default เกือบทั้งหมดดีพอ — มี "Adjusting your PATH environment" → เลือก "Git from the command line and also from 3rd-party software"
- "Choosing the default editor used by Git" → เลือก "Use Visual Studio Code as Git's default editor" (ลง VS Code ไว้แล้ว)
-
"Choose the default branch name"
→ เลือก "Override the default branch name for new repositories" · พิมพ์
main· GitHub ใช้mainเป็น default · ตรงกัน - ส่วนที่เหลือ — กด Next ไปเรื่อย ๆ
Mac
-
ตรวจก่อน — Mac ส่วนใหญ่มี Git ติดมาด้วย
git --version # git version 2.39.x (Apple Git-xxx) → มีแล้ว ข้ามได้ -
ถ้าไม่มี — ผ่าน Xcode Command Line Tools
Mac จะ popup ขออนุญาตลง · ใช้เวลา ~5 นาทีxcode-select --install -
หรือผ่าน Homebrew
brew install git
ตรวจสอบ
git --version
# ต้องเห็น: git version 2.40.x หรือใหม่กว่า
👤 ขั้นที่ 2 — Configure Git (ครั้งเดียวจบ)
บอก Git ว่าใครเป็นคนทำการเปลี่ยนแปลง · ข้อมูลนี้จะติดในทุก commit:
git config --global user.name "Thummaros Ratanapongsai"
git config --global user.email "your-email@ubu.ac.th"
# ตรวจว่าตั้งถูก
git config --global --list
ใช้ email เดียวกับ GitHub
ใช้ email ที่จะ register GitHub · ถ้าใช้คนละ email — commit จะไม่ขึ้นเชื่อมโยงกับ profile GitHub
เพิ่ม config ที่แนะนำ
# ใช้ main เป็น default branch
git config --global init.defaultBranch main
# สีสันใน terminal
git config --global color.ui auto
# แก้ Windows line-ending ให้เรียบร้อย (Windows only)
git config --global core.autocrlf true
🐙 ขั้นที่ 3 — สร้างบัญชี GitHub
-
ไปที่ github.com → Sign up
— ฟรี · ใช้ email เดียวกับที่ตั้งใน
git config user.email -
ตั้ง username
— สั้น · จำง่าย · มืออาชีพ (อย่า
cute_unicorn_2025) · username คือ "portfolio URL" ของคุณ - ยืนยัน email — GitHub ส่งลิงก์มา · กดยืนยัน
- (แนะนำ) เปิด 2FA — Settings → Password and authentication → Two-factor authentication · ใช้ Google Authenticator / Authy · "ป้องกัน account หาย"
🔑 ขั้นที่ 4 — ให้ Git บนเครื่องคุยกับ GitHub ได้
GitHub ไม่ยอมให้ git push ด้วย password ตั้งแต่ 2021 · ต้องใช้ Personal Access Token (PAT) หรือ SSH Key
ทางง่ายที่สุด — Git Credential Manager (Windows/Mac)
- Git for Windows มี Credential Manager ติดมาด้วย · ครั้งแรกที่
git pushจะเด้ง browser ให้ login GitHub · กด Authorize → จำให้ตลอดไป - Mac: Xcode CLI / brew git ก็มีเหมือนกัน · จะใช้ Keychain เก็บ token
ทางเลือก — Personal Access Token (PAT)
- GitHub → Settings → Developer settings → Personal access tokens (classic) → Generate new token
- Note:
cp-course-laptop· Expiration:90 days - เลือก scope:
repo+workflow - กด Generate · copy token ทันที (เห็นครั้งเดียว!)
- ครั้งแรกที่
git pushถาม password → paste token (ไม่ใช่ password GitHub)
🚀 ขั้นที่ 5 — Workflow แรก (5 คำสั่งที่ใช้ 90%)
สมมุติ workflow ของการเริ่ม project ใหม่ที่จะ push ขึ้น GitHub:
วิธีที่ 1 — เริ่มจาก local แล้ว push ขึ้น GitHub
# 1. cd เข้า folder project
cd cp-w04
# 2. init Git ใน folder นี้ (ทำครั้งเดียว)
git init
# 3. ดูสถานะ — ตอนนี้ทุกไฟล์เป็น "untracked"
git status
# 4. เพิ่มไฟล์ทั้งหมดเข้า "staging area"
git add .
# หรือเฉพาะไฟล์:
git add hello.py grade_calc.py
# 5. commit — บันทึก snapshot · ใส่ข้อความสั้น ๆ
git commit -m "first version of grade calculator"
# 6. ไปสร้าง repo เปล่าบน GitHub (ไม่ติ๊ก "Add README")
# https://github.com/new
# 7. เชื่อม local ↔ GitHub (URL ดูจากหน้า repo ที่สร้าง)
git remote add origin https://github.com/USERNAME/cp-w04.git
# 8. push ขึ้น GitHub
git branch -M main # ตั้งชื่อ branch หลักเป็น main
git push -u origin main
วิธีที่ 2 — สร้างบน GitHub ก่อน · clone ลง local
# 1. สร้าง repo บน github.com/new
# ติ๊ก "Add a README" · กด Create
# 2. ที่หน้า repo · กดปุ่ม "Code" → Copy URL
# 3. ใน terminal local — clone
git clone https://github.com/USERNAME/cp-w04.git
cd cp-w04
# พร้อมเขียน code · commit + push ได้เลย
🔁 Loop ที่ใช้ทุกวัน — แก้ → commit → push
# 1. แก้ไขไฟล์ใน VS Code · Save
# 2. ดูว่ามีอะไรเปลี่ยน
git status
# 3. ดู diff (เพิ่ม/ลบบรรทัดไหน)
git diff
# 4. add + commit
git add .
git commit -m "fix bug in grade calc for score over 100"
# 5. push ขึ้น GitHub
git push
เขียน commit message ที่ดี
- ✅ "add CSV import to grade calc"
- ✅ "fix off-by-one error in score loop"
- ❌ "update" — ไม่บอกอะไร
- ❌ "asdf" — เพื่อนตามไม่ได้
📂 .gitignore — บอก Git ว่าไฟล์ไหนไม่ต้องเก็บ
สร้างไฟล์ชื่อ .gitignore ใน root ของ project · ทุกไฟล์ที่ Match จะถูกข้าม:
# Python
__pycache__/
*.pyc
*.pyo
.venv/
venv/
# Secrets — ห้าม commit
.env
*.key
credentials.json
# OS
.DS_Store # Mac
Thumbs.db # Windows
# IDE
.vscode/
.idea/
# Data ใหญ่
*.csv
data/raw/
*.sqlite
# Output
output/
logs/
⚠️ ตรวจก่อน commit ครั้งแรก
git status · ดูว่า "ไม่มี .env / token / password" ใน list · ถ้ามี → เพิ่มใน .gitignore ก่อน
· "commit secrets เข้า GitHub = ของหลุดทันที"
🧰 ดู History · ย้อนกลับ
# ดู log commits ทั้งหมด
git log
git log --oneline # สั้น ๆ
git log --oneline -5 # 5 commits ล่าสุด
# ดู diff ของ commit เดียว
git show abc1234
# ดูไฟล์ใน commit เก่า (ดูเฉย ๆ ไม่แก้)
git show abc1234:hello.py
# ย้อนไฟล์เดียวกลับสู่ commit เก่า
git checkout abc1234 -- hello.py
# ยกเลิก changes ที่ยังไม่ commit
git restore hello.py # ของ Git ใหม่ (2.23+)
git checkout -- hello.py # ของเก่า · ทำงานเหมือนกัน
🌿 Branch — ทดลองโดยไม่กระทบ main
# ดู branch ทั้งหมด
git branch
# สร้าง branch ใหม่ + switch ไปเลย
git checkout -b add-csv-import
# ทำงาน · commit ตามปกติ
git add .
git commit -m "add CSV import"
# กลับมา main
git checkout main
# รวม branch กลับเข้า main
git merge add-csv-import
# ลบ branch ที่ใช้เสร็จแล้ว
git branch -d add-csv-import
📚 ใน W04–W10 ส่วนใหญ่ทำงาน main · branch จะใช้จริงตอน W14 (deploy) + W15 (project ใหญ่)
🔧 ปัญหาที่พบบ่อย
1. fatal: not a git repository
- ลืม
git init· หรืออยู่ผิด folder ·pwdตรวจก่อน
2. fatal: remote origin already exists
git remote remove origin
git remote add origin https://github.com/USERNAME/repo.git
3. error: failed to push some refs
- GitHub มีการเปลี่ยนแปลง (เช่น เพิ่ม README ตอนสร้าง) ที่ local ไม่มี ·
git pull --rebaseก่อน push
4. push แล้วถาม password — Authentication failed
- GitHub ไม่รับ password แบบเก่าตั้งแต่ 2021 · ใช้ PAT (ดูขั้นที่ 4) แทน password
- หรือใช้ Credential Manager → ครั้งแรกจะเด้ง browser ให้ authorize
5. เผลอ commit .env ที่มี secret
- ถ้ายังไม่ push:
git rm --cached .env+ เพิ่ม .env ใน .gitignore + commit - ถ้า push ไปแล้ว: token รั่วแล้ว · "ต้องเปลี่ยน token / password ทันที"
- ใช้ git-filter-repo ลบจาก history (ขั้น advanced)
6. ลืมว่า commit ไหนเปลี่ยนอะไร
git log --oneline --all
git reflog # log ของ HEAD movement · กู้ commit หายได้
🤖 ใช้ AI ช่วยกับ Git
Prompts ที่ใช้ได้
- "git status บอกว่า untracked + modified + staged ต่างกันยังไง?"
- "ช่วยเขียน commit message สำหรับ diff นี้: [paste]"
- "ลบ commit ที่มี secret ออกจาก history ทำยังไง?"
- "merge conflict ใน hello.py · ช่วยอธิบายว่า <<<<<<< ===== >>>>>>> หมายความว่ายังไง"
- "undo commit ล่าสุดแต่เก็บ changes ใน working dir ทำยังไง?"
🎨 GUI Alternative — สำหรับคนที่ไม่ชอบ terminal
| เครื่องมือ | ดี | เหมาะกับ |
|---|---|---|
| VS Code Source Control | ในตัว · ไม่ต้องลงอะไรเพิ่ม | ทุกคนในคอร์สนี้ |
| GitHub Desktop | UI ง่าย · clone/push ปุ่มเดียว | คนเริ่มต้น |
| GitKraken | visualize branch สวย | project ที่ branch เยอะ |
VS Code Source Control — ใช้ได้ทันที
หลัง
git init ใน folder · กด icon ⎇ ใน sidebar ซ้าย ·
เห็นไฟล์เปลี่ยน · กด + เพื่อ stage · พิมพ์ commit message · กดเครื่องหมายถูก · กด ··· → Push
🎯 Workshop — ครั้งแรกของคุณ (15 นาที)
-
สร้าง repo บน GitHub
— github.com/new · ชื่อ
cp-w04-test· ติ๊ก "Add a README" · Create -
clone ลง local
— copy URL → terminal:
git clone https://github.com/USERNAME/cp-w04-test.git -
cd เข้า folder + เปิดใน VS Code
cd cp-w04-test && code . -
สร้าง
hello.pyใส่print("first commit!") -
commit + push
git add . git commit -m "add hello.py" git push - เช็คบน GitHub — refresh หน้า repo · เห็น hello.py ✅
-
แก้ + push ครั้งที่ 2
— เพิ่มบรรทัด · save ·
git add . && git commit -m "add greeting" && git push - ดู history — บน GitHub: ปุ่ม "Commits" · เห็น 2 commit ที่ทำไป
🎯 Checklist ก่อนไปต่อ
- ☐
git --versionทำงาน - ☐
git config user.name / user.emailตั้งแล้ว - ☐ มี GitHub account + 2FA
- ☐
git pushครั้งแรกสำเร็จ (Workshop ข้อ 5) - ☐ มี
.gitignoreใน project (อย่างน้อยมี.env)