🚀 คู่มือ Use Case การใช้งาน Git ในการพัฒนา Web Application

📑 สารบัญ


1ส่วนที่ 1: การพัฒนาคนเดียว (Solo Development)

1.1 เริ่มต้นโปรเจ็กต์ใหม่

📦 Use Case: สร้างโปรเจ็กต์ Web Application ใหม่

สถานการณ์: เริ่มต้นพัฒนา web application ใหม่บนเครื่องตนเอง
ขั้นตอน:
# 1. สร้าง directory สำหรับโปรเจ็กต์
mkdir my-web-app
cd my-web-app

# 2. เริ่มต้น Git repository
git init

# 3. สร้างไฟล์โครงสร้างพื้นฐาน
mkdir src public
touch README.md .gitignore

# 4. สร้าง .gitignore
echo "node_modules/
.env
dist/
.DS_Store
*.log" > .gitignore

# 5. สร้าง README
echo "# My Web Application" > README.md

# 6. เพิ่มไฟล์เข้า staging area
git add .

# 7. สร้าง initial commit
git commit -m "Initial commit: project setup"

ผลลัพธ์: มี Git repository พร้อมใช้งานบนเครื่อง

1.2 บันทึกการเปลี่ยนแปลง (Basic Workflow)

📝 Use Case: พัฒนา feature และบันทึกการเปลี่ยนแปลง

สถานการณ์: เขียนโค้ด HTML, CSS, JavaScript และต้องการบันทึก
ขั้นตอน:
# 1. ตรวจสอบสถานะไฟล์
git status

# 2. ดูการเปลี่ยนแปลงของไฟล์
git diff

# 3. เพิ่มไฟล์ที่ต้องการ commit
git add index.html styles.css
# หรือเพิ่มทั้งหมด
git add .

# 4. ตรวจสอบว่าไฟล์อยู่ใน staging area แล้ว
git status

# 5. Commit พร้อม message
git commit -m "Add homepage with responsive design"

# 6. ดูประวัติ commit
git log --oneline

1.3 แก้ไขและยกเลิกการเปลี่ยนแปลง

❌ Use Case 3.1: ยกเลิกการแก้ไขไฟล์ที่ยังไม่ได้ add

สถานการณ์: แก้ไขไฟล์แล้วเปลี่ยนใจ ต้องการกลับไปเหมือนเดิม
# ดูไฟล์ที่เปลี่ยนแปลง
git status

# ยกเลิกการแก้ไขไฟล์เดียว
git checkout -- app.js

# หรือยกเลิกทั้งหมด
git checkout -- .

# Git 2.23+: ใช้คำสั่งใหม่
git restore app.js

⚙️ Use Case 3.2: ยกเลิกไฟล์ที่ add แล้วใน staging area

# ยกเลิกการ add ไฟล์เดียว
git reset HEAD config.js

# ยกเลิกทั้งหมด
git reset HEAD .

# Git 2.23+: ใช้คำสั่งใหม่
git restore --staged config.js

1.4 การทำงานกับ Branches

🌿 Use Case 4.1: สร้าง branch สำหรับ feature ใหม่

สถานการณ์: ต้องการพัฒนา feature ใหม่โดยไม่กระทบ main branch
# ดู branch ปัจจุบัน
git branch

# สร้าง branch ใหม่
git branch feature/user-profile

# สลับไปยัง branch ใหม่
git checkout feature/user-profile

# หรือสร้างและสลับในคำสั่งเดียว
git checkout -b feature/user-profile

# Git 2.23+: ใช้คำสั่งใหม่
git switch -c feature/user-profile

🔀 Use Case 4.2: รวม branch (Merge)

สถานการณ์: พัฒนา feature เสร็จแล้ว ต้องการรวมเข้า main branch
# กลับไปที่ main branch
git checkout main

# รวม feature branch เข้ามา
git merge feature/user-profile

# ดู merge ที่เกิดขึ้น
git log --oneline --graph

# ลบ branch ที่ไม่ใช้แล้ว
git branch -d feature/user-profile

1.5 การทำงานกับ Remote Repository

🌐 Use Case 5.1: เชื่อมต่อกับ GitHub/GitLab

สถานการณ์: ต้องการ backup โค้ดไปยัง remote repository
# เพิ่ม remote repository
git remote add origin https://github.com/username/my-web-app.git

# ตรวจสอบ remote
git remote -v

# Push ครั้งแรก
git push -u origin main

# Push branch อื่นๆ
git push -u origin feature/user-profile

📥 Use Case 5.2: ดึงข้อมูลจาก Remote

สถานการณ์: ทำงานต่อจากเครื่องอื่น หรือมีการเปลี่ยนแปลงบน remote
# Clone repository มาครั้งแรก
git clone https://github.com/username/my-web-app.git

# ดึงข้อมูลอัพเดท
git fetch origin

# ดึงและรวมข้อมูล
git pull origin main

# Pull branch อื่น
git pull origin feature/payment-system

1.6 การจัดการ Tags (Versioning)

🏷️ Use Case 6.1: สร้าง tag สำหรับ release

สถานการณ์: พร้อม deploy version 1.0.0
# สร้าง lightweight tag
git tag v1.0.0

# สร้าง annotated tag (แนะนำ)
git tag -a v1.0.0 -m "Release version 1.0.0 - Initial release"

# ดู tags ทั้งหมด
git tag

# Push tag ไปยัง remote
git push origin v1.0.0

# Push tags ทั้งหมด
git push origin --tags

1.7 การดูประวัติและค้นหา

📜 Use Case 7.1: ดูประวัติการเปลี่ยนแปลง

# ดู log พื้นฐาน
git log

# ดูแบบสั้นกระทัดรัด
git log --oneline

# ดูแบบกราฟ
git log --graph --oneline --all

# ดู log ของไฟล์เฉพาะ
git log -- index.html

# ดู log พร้อม diff
git log -p

# ดู log จำนวนจำกัด
git log -n 5

1.8 Stash: บันทึกงานชั่วคราว

💾 Use Case 8.1: เก็บการเปลี่ยนแปลงชั่วคราว

สถานการณ์: กำลังแก้ไขอยู่ แต่ต้องสลับไปทำอย่างอื่นก่อน
# เก็บการเปลี่ยนแปลง
git stash

# เก็บพร้อม message
git stash save "WIP: working on login form"

# เก็บรวมทั้งไฟล์ที่ยังไม่ track
git stash -u

# ดู stash ทั้งหมด
git stash list

# นำ stash กลับมาใช้
git stash apply

# นำ stash กลับมาและลบ stash
git stash pop

# ลบ stash
git stash drop stash@{0}

2ส่วนที่ 2: การพัฒนาเป็นทีม (Team Development)

2.1 Workflow พื้นฐานสำหรับทีม

👥 Use Case 11: Clone และเริ่มต้นทำงาน

สถานการณ์: เข้าร่วมโปรเจ็กต์ที่มีอยู่แล้ว
# Clone repository
git clone https://github.com/company/project.git
cd project

# ดู branches ทั้งหมดรวม remote
git branch -a

# Checkout branch ที่ต้องการทำงาน
git checkout develop

# อัพเดทข้อมูลล่าสุด
git pull origin develop

2.2 Feature Branch Workflow

✨ Use Case 12: พัฒนา Feature ใหม่

สถานการณ์: รับ task พัฒนา feature ใหม่
# 1. อัพเดท develop branch
git checkout develop
git pull origin develop

# 2. สร้าง feature branch จาก develop
git checkout -b feature/add-shopping-cart

# 3. พัฒนา feature
git add .
git commit -m "Implement shopping cart functionality"

# 4. Push feature branch
git push -u origin feature/add-shopping-cart

2.3 Pull Request (PR) / Merge Request (MR) Workflow

🔔 Use Case 13: สร้าง Pull Request

ขั้นตอนบน GitHub/GitLab:
  1. Push feature branch ไปยัง remote
  2. เปิด Pull Request บนเว็บ
  3. เลือก base branch (develop) และ compare branch (feature/xxx)
  4. เขียนคำอธิบาย PR
  5. Assign reviewers
  6. รอ code review และแก้ไขตาม feedback

2.4 Code Review Process

👀 Use Case 14: Review โค้ดของเพื่อนร่วมทีม

ขั้นตอนการ Review:
# 1. Fetch PR branch มาดูบนเครื่องตัวเอง
git fetch origin
git checkout feature/teammate-feature
git pull origin feature/teammate-feature

# 2. ดูการเปลี่ยนแปลง
git diff develop...feature/teammate-feature

# 3. ทดสอบโค้ด
npm install
npm run dev

# 4. ดู commits
git log develop..feature/teammate-feature

2.5 Rebase Workflow

🔄 Use Case 16: ใช้ Rebase แทน Merge

สถานการณ์: ต้องการประวัติที่เป็นเส้นตรง ไม่มี merge commits
# 1. อัพเดท develop
git checkout develop
git pull origin develop

# 2. กลับไปที่ feature branch
git checkout feature/user-settings

# 3. Rebase แทนการ merge
git rebase develop

# 4. ถ้ามี conflict แก้ไขทีละ commit
git add .
git rebase --continue

# 5. Force push
git push -f origin feature/user-settings

2.6 Git Flow Pattern

🌊 Use Case 17: ใช้ Git Flow เป็น Workflow

โครงสร้าง Branches:

2.7 GitHub Flow (Simplified)

⚡ Use Case 18: ใช้ GitHub Flow

# 1. อัพเดท main
git checkout main
git pull origin main

# 2. สร้าง feature branch จาก main
git checkout -b feature/add-comments

# 3. Commit บ่อยๆ
git add .
git commit -m "Add comment form UI"

# 4. Push และสร้าง PR
git push -u origin feature/add-comments

# 5. Merge เข้า main (ผ่าน GitHub UI)

# 6. Deploy production จาก main

3ส่วนที่ 3: Use Cases ขั้นสูง (Advanced Use Cases)

3.1 Bisect: ค้นหา Commit ที่ทำให้เกิด Bug

🔬 Use Case 26: ใช้ Git Bisect หา Bug

สถานการณ์: รู้ว่ามี bug แต่ไม่รู้ว่า commit ไหนทำให้เกิด
# 1. เริ่ม bisect
git bisect start

# 2. บอกว่า commit ปัจจุบันมี bug
git bisect bad

# 3. บอก commit ที่แน่ใจว่าไม่มี bug
git bisect good v1.0.0

# 4. Git จะ checkout ไปยัง commit ตรงกลาง
# ทดสอบว่ามี bug หรือไม่
git bisect bad  # หรือ git bisect good

# 5. ทำซ้ำจนเจอ commit ที่ทำให้เกิด bug
git bisect reset

3.2 Worktree: ทำงานหลาย Branch พร้อมกัน

🌳 Use Case 27: ใช้ Git Worktree

สถานการณ์: ต้องการทำงานสองงานพร้อมกันโดยไม่ต้องสลับ branch
# สร้าง worktree ใหม่
git worktree add ../project-hotfix hotfix/urgent-bug

# ดู worktrees ทั้งหมด
git worktree list

# ทำงานใน worktree ใหม่
cd ../project-hotfix
git add .
git commit -m "Fix urgent bug"
git push origin hotfix/urgent-bug

# ลบ worktree
git worktree remove ../project-hotfix

3.3 Reflog: กู้คืนงานที่หายไป

💡 Use Case 28: กู้คืน Commits หรือ Branches ที่ลบไป

# ดู reflog
git reflog

# กู้คืน commit ที่ reset ไป
git reset --hard def5678

# สร้าง branch จาก commit ที่หายไป
git branch recovered-work def5678

# กู้คืน branch ที่ถูกลบ
git reflog show branch-name
git checkout -b branch-name HEAD@{n}
💡 หมายเหตุ: Reflog จะเก็บข้อมูล 90 วัน (default)

3.4 Security Best Practices

🔒 Use Case 32: ตรวจสอบ Secrets และ Security

# 1. ใช้ git-secrets ป้องกัน commit secrets
git secrets --install
git secrets --register-aws

# 2. Scan ประวัติหา secrets
git log -p | grep -i "password\|api_key\|secret"

# 3. ลบ sensitive data ถ้าเจอ
git filter-repo --path .env --invert-paths
git push origin --force --all
⚠️ สำคัญ: ไม่ commit secrets, API keys, passwords ลงไปใน repository

3.5 Performance Optimization

⚡ Use Case 33: เพิ่มประสิทธิภาพ Git

# 1. Garbage Collection
git gc --aggressive --prune=now

# 2. ทำความสะอาด repository
git reflog expire --expire=now --all
git gc --prune=now

# 3. Shallow clone (สำหรับ CI/CD)
git clone --depth 1 https://github.com/company/project.git

# 4. Partial clone
git clone --filter=blob:none https://github.com/company/project.git

# 5. ตั้งค่า cache credentials
git config --global credential.helper cache

✅ Best Practices สรุป

Commit Messages

<type>(<scope>): <subject>

<body>

<footer>
Types:

Branch Naming

feature/description      # Feature ใหม่
bugfix/description       # แก้ bug
hotfix/description       # แก้ bug ด่วน
release/version          # เตรียม release

Security Tips

Performance Tips


🛠️ เครื่องมือเสริม

Git GUI Tools

CLI Tools