Git เป็นระบบ Version Control ที่ใช้มากที่สุดในโลก ไม่ว่าจะเป็นบริษัทเล็กหรือใหญ่ Open Source หรือ Enterprise ทุกที่ใช้ Git ในปี 2026 การไม่รู้ Git เท่ากับไม่สามารถทำงานร่วมกับทีมพัฒนาซอฟต์แวร์ได้
บทความนี้จะสอน Git ตั้งแต่พื้นฐานจนถึง Workflow ที่ใช้ในทีมจริง ครอบคลุม Branch, Merge, Rebase, Conflict Resolution และ GitHub Collaboration
Git คืออะไร?
Git คือ Distributed Version Control System (DVCS) ที่สร้างโดย Linus Torvalds (ผู้สร้าง Linux) ในปี 2005 ทำหน้าที่บันทึกประวัติการเปลี่ยนแปลงของ Source Code ทุกบรรทัด ทำให้สามารถ:
- ย้อนกลับไปเวอร์ชันเก่าได้ทุกเมื่อ
- ทำงานหลายฟีเจอร์พร้อมกัน (Branching)
- ทำงานร่วมกันหลายคนโดยไม่ชนกัน
- ดูว่าใครแก้ไขอะไร เมื่อไหร่ ทำไม
ติดตั้ง Git
# Windows — ดาวน์โหลดจาก git-scm.com
# Mac
brew install git
# Linux (Ubuntu/Debian)
sudo apt install git
# ตั้งค่าเริ่มต้น (ทำครั้งเดียว)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main
# ตรวจสอบ
git --version
git config --list
คำสั่ง Git พื้นฐาน
เริ่มต้นโปรเจกต์
# สร้าง Repository ใหม่
mkdir my-project && cd my-project
git init
# หรือ Clone จาก GitHub
git clone https://github.com/user/repo.git
cd repo
Workflow พื้นฐาน: Add → Commit → Push
# 1. ดูสถานะ
git status
# 2. เพิ่มไฟล์เข้า Staging Area
git add filename.txt # เพิ่มไฟล์เดียว
git add . # เพิ่มทั้งหมด
# 3. Commit
git commit -m "Add login feature"
# 4. Push ไป Remote
git push origin main
# 5. Pull จาก Remote
git pull origin main
Branch — หัวใจของ Git
Branch ช่วยให้คุณทำงานหลายฟีเจอร์พร้อมกันโดยไม่กระทบ Code หลัก
# สร้าง Branch ใหม่
git branch feature/login
# สลับไป Branch นั้น
git checkout feature/login
# หรือ (Git 2.23+)
git switch feature/login
# สร้าง + สลับในคำสั่งเดียว
git checkout -b feature/login
git switch -c feature/login
# ดู Branch ทั้งหมด
git branch # local
git branch -a # local + remote
# ลบ Branch
git branch -d feature/login # ลบ (safe)
git branch -D feature/login # ลบ (force)
Merge — รวม Branch
# สลับไป main
git switch main
# Merge feature branch เข้า main
git merge feature/login
# ถ้ามี Conflict:
# 1. Git จะบอกว่าไฟล์ไหน Conflict
# 2. เปิดไฟล์ แก้ไข Conflict markers (<<<<<<<, =======, >>>>>>>)
# 3. git add แล้ว git commit
# Merge แบบ No-fast-forward (สร้าง Merge commit เสมอ)
git merge --no-ff feature/login
Rebase — ทางเลือกของ Merge
# Rebase feature branch กับ main
git switch feature/login
git rebase main
# Interactive Rebase (จัดเรียง/รวม commits)
git rebase -i HEAD~3
# ความแตกต่าง:
# Merge: สร้าง Merge commit, เห็นประวัติแยก Branch
# Rebase: ประวัติเป็นเส้นตรง สะอาดกว่า
Git Log — ดูประวัติ
# ดูประวัติ
git log
git log --oneline # ย่อ
git log --oneline --graph # แสดง Branch graph
git log --oneline --graph --all # ทุก Branch
git log -5 # 5 commits ล่าสุด
git log --author="name" # เฉพาะคนนี้
# ดูว่าใครแก้ไขบรรทัดไหน
git blame filename.txt
# ดูความเปลี่ยนแปลง
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # เปรียบเทียบ 2 branch
GitHub Workflow สำหรับทีม
GitHub Flow (Simple)
- สร้าง Branch จาก
main - Commit + Push ขึ้น GitHub
- สร้าง Pull Request (PR)
- Code Review + Discussion
- Merge เข้า
main - Deploy จาก
main
# ขั้นตอนจริง:
git switch -c feature/new-api
# ... เขียน code ...
git add .
git commit -m "Add new API endpoint"
git push -u origin feature/new-api
# → ไปสร้าง PR บน GitHub
# → ทีม Review
# → Merge PR
# → ลบ Branch
Git Flow (Complex)
สำหรับทีมใหญ่ที่มี Release cycle:
main— Production code เท่านั้นdevelop— Development integrationfeature/*— ฟีเจอร์ใหม่release/*— เตรียม Releasehotfix/*— แก้ Bug ด่วนใน Production
เทคนิคที่ใช้บ่อย
Stash — เก็บงานชั่วคราว
# เก็บ Changes ไว้ก่อน (ไม่ Commit)
git stash
git stash save "WIP: login form"
# ดู Stash list
git stash list
# เอากลับมา
git stash pop # เอาอันล่าสุด + ลบ
git stash apply # เอาอันล่าสุด + ไม่ลบ
git stash drop # ลบ Stash
Cherry-pick — เลือก Commit
# เอา Commit จาก Branch อื่นมาใส่
git cherry-pick abc1234
Reset vs Revert
# Reset — ย้อนกลับ (อันตราย ลบประวัติ)
git reset --soft HEAD~1 # ย้อน 1 commit (เก็บ Changes)
git reset --hard HEAD~1 # ย้อน 1 commit (ลบ Changes)
# Revert — สร้าง Commit ใหม่ที่ Undo (ปลอดภัย)
git revert abc1234 # สร้าง Commit ที่ยกเลิก abc1234
.gitignore — ไฟล์ที่ไม่ต้อง Track
# .gitignore
node_modules/
__pycache__/
*.pyc
.env
.env.local
dist/
build/
*.log
.DS_Store
Thumbs.db
.idea/
.vscode/
คำสั่ง Git ที่ใช้บ่อย (Cheatsheet)
| คำสั่ง | หน้าที่ |
|---|---|
git init | สร้าง Repository |
git clone URL | Clone Repository |
git status | ดูสถานะ |
git add . | Stage ทั้งหมด |
git commit -m "msg" | Commit |
git push | Push ไป Remote |
git pull | Pull จาก Remote |
git switch -c branch | สร้าง + สลับ Branch |
git merge branch | Merge Branch |
git log --oneline | ดูประวัติ |
git stash | เก็บงานชั่วคราว |
git revert HASH | Undo Commit (safe) |
สรุป
Git เป็นเครื่องมือที่ทรงพลังและจำเป็นสำหรับนักพัฒนาทุกคน ไม่ว่าจะทำงานคนเดียวหรือในทีม 100 คน การเข้าใจ Git ไม่เพียงช่วยให้จัดการ Code ได้ดีขึ้น แต่ยังเป็นทักษะพื้นฐานที่ทุกบริษัท Tech ต้องการ
เริ่มต้นวันนี้: สร้าง GitHub Account, สร้าง Repository แรก, Commit Code แรก แล้วคุณจะเข้าใจว่าทำไม Git ถึงเปลี่ยนโลกของ Software Development ไปตลอดกาล
