Setup · เซฟผลงาน · กลับไปย้อนได้ · ของคุณตลอดชีวิต

🌿 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

  1. ดาวน์โหลด git-scm.com/download/win — โหลดอัตโนมัติ Git for Windows installer
  2. รัน installer · ค่า default เกือบทั้งหมดดีพอ — มี "Adjusting your PATH environment" → เลือก "Git from the command line and also from 3rd-party software"
  3. "Choosing the default editor used by Git" → เลือก "Use Visual Studio Code as Git's default editor" (ลง VS Code ไว้แล้ว)
  4. "Choose the default branch name" → เลือก "Override the default branch name for new repositories" · พิมพ์ main · GitHub ใช้ main เป็น default · ตรงกัน
  5. ส่วนที่เหลือ — กด Next ไปเรื่อย ๆ

Mac

  1. ตรวจก่อน — Mac ส่วนใหญ่มี Git ติดมาด้วย
    git --version
    # git version 2.39.x (Apple Git-xxx) → มีแล้ว ข้ามได้
  2. ถ้าไม่มี — ผ่าน Xcode Command Line Tools
    xcode-select --install
    Mac จะ popup ขออนุญาตลง · ใช้เวลา ~5 นาที
  3. หรือผ่าน 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

  1. ไปที่ github.com → Sign up — ฟรี · ใช้ email เดียวกับที่ตั้งใน git config user.email
  2. ตั้ง username — สั้น · จำง่าย · มืออาชีพ (อย่า cute_unicorn_2025) · username คือ "portfolio URL" ของคุณ
  3. ยืนยัน email — GitHub ส่งลิงก์มา · กดยืนยัน
  4. (แนะนำ) เปิด 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)

ทางเลือก — Personal Access Token (PAT)

  1. GitHub → Settings → Developer settings → Personal access tokens (classic) → Generate new token
  2. Note: cp-course-laptop · Expiration: 90 days
  3. เลือก scope: repo + workflow
  4. กด Generate · copy token ทันที (เห็นครั้งเดียว!)
  5. ครั้งแรกที่ 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" — เพื่อนตามไม่ได้
เริ่มด้วย verb (add, fix, update, remove, refactor) · บอกว่า "ทำอะไร"

📂 .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

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

4. push แล้วถาม password — Authentication failed

5. เผลอ commit .env ที่มี secret

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 DesktopUI ง่าย · clone/push ปุ่มเดียวคนเริ่มต้น
GitKrakenvisualize branch สวยproject ที่ branch เยอะ
VS Code Source Control — ใช้ได้ทันที หลัง git init ใน folder · กด icon ⎇ ใน sidebar ซ้าย · เห็นไฟล์เปลี่ยน · กด + เพื่อ stage · พิมพ์ commit message · กดเครื่องหมายถูก · กด ··· → Push

🎯 Workshop — ครั้งแรกของคุณ (15 นาที)

  1. สร้าง repo บน GitHub — github.com/new · ชื่อ cp-w04-test · ติ๊ก "Add a README" · Create
  2. clone ลง local — copy URL → terminal: git clone https://github.com/USERNAME/cp-w04-test.git
  3. cd เข้า folder + เปิดใน VS Code cd cp-w04-test && code .
  4. สร้าง hello.py ใส่ print("first commit!")
  5. commit + push
    git add .
    git commit -m "add hello.py"
    git push
  6. เช็คบน GitHub — refresh หน้า repo · เห็น hello.py ✅
  7. แก้ + push ครั้งที่ 2 — เพิ่มบรรทัด · save · git add . && git commit -m "add greeting" && git push
  8. ดู history — บน GitHub: ปุ่ม "Commits" · เห็น 2 commit ที่ทำไป

🎯 Checklist ก่อนไปต่อ