
ทำไม IT ต้องรู้ Git?
ถ้าคุณเป็น IT ที่ทำงานดูแลระบบ เขียน Script จัดการ Config Files หรือแม้แต่เขียน Documentation แล้วยังไม่เคยใช้ Version Control ปี 2026 เป็นเวลาที่ต้องเริ่มแล้ว ไม่ใช่แค่ Developer เท่านั้นที่ต้องใช้ Git — IT Ops, System Admin, Network Engineer ทุกคนได้ประโยชน์จาก Git
ปัญหาที่เกิดจากการไม่ใช้ Version Control
- ไฟล์ Config ถูกแก้ไขแล้วไม่รู้ว่าใครแก้ เมื่อไหร่ แก้อะไร
- Script เวอร์ชันเก่าที่ใช้งานได้ถูกเขียนทับ ย้อนกลับไม่ได้
- มีไฟล์
config_v1.txt,config_v2_final.txt,config_v2_final_real.txtเต็ม Folder - ทำงานร่วมกันหลายคน ไฟล์ชนกันตลอด
- เซิร์ฟเวอร์มีปัญหาหลังเปลี่ยน Config ไม่รู้ว่าต้องย้อนกลับไปเวอร์ชันไหน
Git แก้ปัญหาทั้งหมดนี้ ด้วยการบันทึกประวัติการเปลี่ยนแปลงทุกครั้ง ย้อนกลับได้ทุกเมื่อ และทำงานร่วมกันได้โดยไม่ชน
Git คืออะไร? (อธิบายแบบง่ายที่สุด)
Git คือ “ระบบบันทึกประวัติการเปลี่ยนแปลงของไฟล์” (Version Control System) คิดง่าย ๆ เหมือน:
- Save Point ในเกม: คุณ Save ได้ทุกจุด ถ้าตายก็โหลดกลับมา
- Google Docs History: ดูว่าใครแก้ไขอะไร เมื่อไหร่ ย้อนกลับได้
- Time Machine ของ Mac: ย้อนเวลากลับไปดูไฟล์ตอนเก่า ๆ
แต่ Git ทรงพลังกว่า เพราะ:
- ทำงาน Offline ได้ (ไม่ต้องต่อ Internet)
- รองรับการทำงานหลายคนพร้อมกัน
- มี Branching — แยกทดลองได้โดยไม่กระทบของเดิม
- ฟรี และ Open Source
ติดตั้ง Git
Windows
# ดาวน์โหลดจาก https://git-scm.com/download/win
# ติดตั้ง → Next ไปเรื่อย ๆ (Default ทั้งหมด)
# เปิด Git Bash หรือ PowerShell
# ตรวจสอบ
git --version
# git version 2.44.0.windows.1
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install git -y
git --version
macOS
# ติดตั้งผ่าน Homebrew
brew install git
git --version
ตั้งค่าเริ่มต้น (ทำครั้งเดียว)
# บอก Git ว่าคุณคือใคร
git config --global user.name "Somchai IT"
git config --global user.email "[email protected]"
# ตั้ง Default Branch เป็น main
git config --global init.defaultBranch main
# ตรวจสอบ
git config --list
คำสั่ง Git พื้นฐาน — เริ่มจาก 0
git init — สร้าง Repository ใหม่
# สร้าง Folder สำหรับ Config Files
mkdir server-configs
cd server-configs
# เริ่มต้น Git ใน Folder นี้
git init
# Initialized empty Git repository in .../server-configs/.git/
# ตอนนี้ Git เริ่มติดตามทุกอย่างใน Folder นี้แล้ว!
git clone — Copy Repository ที่มีอยู่
# Clone จาก GitHub
git clone https://github.com/company/infra-configs.git
# Clone จาก GitLab (Self-hosted)
git clone https://gitlab.company.com/it-team/scripts.git
git add — เลือกไฟล์ที่จะบันทึก
# เพิ่มไฟล์เดียว
git add nginx.conf
# เพิ่มหลายไฟล์
git add nginx.conf firewall.rules
# เพิ่มทั้งหมด
git add .
# ดูว่าอะไรถูก Add แล้ว
git status
git commit — บันทึกการเปลี่ยนแปลง (Save Point)
# Commit พร้อมข้อความอธิบาย
git commit -m "Add initial nginx config for web server"
# ข้อความควรบอกว่า "ทำอะไร" ไม่ใช่ "แก้ไข"
# ดี: "Add SSL config for domain.com"
# ดี: "Fix wrong port number in nginx.conf"
# ไม่ดี: "update"
# ไม่ดี: "แก้ไฟล์"
git push — ส่งขึ้น Remote (GitHub/GitLab)
# Push ขึ้น Remote ครั้งแรก
git push -u origin main
# Push ครั้งถัด ๆ ไป
git push
git pull — ดึงจาก Remote มาอัพเดท
# ดึง Changes ล่าสุดจาก Remote
git pull origin main
# หรือ (ถ้าตั้ง tracking แล้ว)
git pull
Workflow พื้นฐาน: Add → Commit → Push
# 1. แก้ไขไฟล์ Config
nano /etc/nginx/nginx.conf
# 2. ดูว่ามีอะไรเปลี่ยน
git status
# modified: nginx.conf
# 3. ดูรายละเอียดที่เปลี่ยน
git diff nginx.conf
# 4. Add ไฟล์ที่แก้ไข
git add nginx.conf
# 5. Commit (บันทึก)
git commit -m "Update nginx worker_connections from 1024 to 2048"
# 6. Push ขึ้น Remote
git push
Branch — แยกทดลองโดยไม่กระทบของเดิม
Branch คืออะไร?
Branch คือ “สำเนาเสมือน” ที่แยกออกมาจากเวอร์ชันหลัก คุณสามารถทดลองแก้ไขใน Branch ได้เต็มที่ ถ้าดีก็รวมกลับ ถ้าไม่ดีก็ทิ้งไป ไม่กระทบ main เลย
# สร้าง Branch ใหม่ + สลับไป
git switch -c feature/new-firewall-rules
# ตอนนี้คุณอยู่ใน Branch "feature/new-firewall-rules"
# แก้ไข ทดลองได้เต็มที่!
# ทำงานเสร็จ → Commit
git add .
git commit -m "Add new firewall rules for port 8443"
# สลับกลับไป main
git switch main
# รวม Branch เข้า main
git merge feature/new-firewall-rules
# ลบ Branch ที่ใช้แล้ว
git branch -d feature/new-firewall-rules
ตัวอย่าง Branch ที่ IT ควรใช้
| Branch Name | ใช้ทำอะไร |
|---|---|
main |
Config ที่ใช้งานจริง (Production) |
feature/new-ssl |
เพิ่ม SSL Certificate ใหม่ |
fix/nginx-502 |
แก้ปัญหา Nginx 502 Bad Gateway |
test/load-balancer |
ทดสอบ Config Load Balancer ใหม่ |
Resolving Conflicts — แก้ไขเมื่อไฟล์ชนกัน
Conflict เกิดเมื่อ 2 คนแก้ไฟล์เดียวกันในบรรทัดเดียวกัน Git จะบอกว่ามี Conflict และให้คุณเลือกว่าจะเอาเวอร์ชันไหน
# ตัวอย่าง Conflict ในไฟล์:
<<<<<<< HEAD
worker_connections 2048;
=======
worker_connections 4096;
>>>>>>> feature/optimize-nginx
# วิธีแก้:
# 1. เลือกว่าจะเอาเวอร์ชันไหน (ลบอีกอัน + ลบ markers)
worker_connections 4096;
# 2. Add ไฟล์ที่แก้แล้ว
git add nginx.conf
# 3. Commit
git commit -m "Resolve conflict: use 4096 worker_connections"
GitHub/GitLab พื้นฐาน
สร้าง Repository บน GitHub
- ไปที่ github.com → Sign In → New Repository
- ตั้งชื่อ เช่น
server-configs - เลือก Private (ไม่อยากให้คนอื่นเห็น Config)
- Create Repository
- ทำตาม Instructions ที่ GitHub แสดง:
# เชื่อม Local Repo กับ GitHub
git remote add origin https://github.com/yourname/server-configs.git
git push -u origin main
GitLab (Self-hosted)
หลายองค์กรใช้ GitLab แทน GitHub เพราะติดตั้งบน Server ภายในได้ ข้อมูลไม่ออกนอกองค์กร
# เชื่อมกับ GitLab
git remote add origin https://gitlab.company.com/it-team/configs.git
git push -u origin main
.gitignore — ไฟล์ที่ไม่ต้อง Track
บางไฟล์ไม่ควรเข้า Git เช่น Password, Private Key, Log Files สร้างไฟล์ .gitignore เพื่อบอก Git ว่าอย่า Track
# .gitignore สำหรับ IT/Sysadmin
# Passwords & Secrets
*.pem
*.key
*.p12
.env
passwords.txt
secrets/
# Log files
*.log
/var/log/
# Temp files
*.tmp
*.bak
*.swp
*~
# OS files
.DS_Store
Thumbs.db
# Editor files
.vscode/
.idea/
แบบฝึกหัด: จัดการ Config Files ด้วย Git
Exercise 1: สร้าง Repository และ Commit แรก
# สร้าง Folder
mkdir my-server-configs
cd my-server-configs
git init
# สร้างไฟล์ Config ตัวอย่าง
echo "server_name example.com;" > nginx.conf
echo "PermitRootLogin no" > sshd_config
# สร้าง .gitignore
echo "*.key" > .gitignore
echo "*.pem" >> .gitignore
# Add + Commit
git add .
git commit -m "Initial commit: add nginx and sshd config"
# ดูประวัติ
git log --oneline
Exercise 2: แก้ไขและ Commit
# แก้ไข nginx.conf
echo "worker_connections 2048;" >> nginx.conf
# ดูว่ามีอะไรเปลี่ยน
git diff
# Commit
git add nginx.conf
git commit -m "Increase worker_connections to 2048"
# ดูประวัติ (จะเห็น 2 Commits)
git log --oneline
Exercise 3: ใช้ Branch ทดลอง
# สร้าง Branch ทดลอง
git switch -c test/new-ssl-config
# เพิ่ม SSL Config
echo "ssl_certificate /etc/ssl/cert.pem;" >> nginx.conf
git add nginx.conf
git commit -m "Add SSL certificate path"
# สลับกลับ main (Config จะกลับเป็นเวอร์ชันเดิม!)
git switch main
# ถ้าพอใจ → Merge
git merge test/new-ssl-config
git branch -d test/new-ssl-config
สร้างนิสัย Git — ทำทุกวัน
| กิจกรรม | คำสั่ง Git | ความถี่ |
|---|---|---|
| แก้ไข Config | git add + git commit |
ทุกครั้งที่แก้ |
| ดูประวัติ | git log --oneline |
เมื่อต้องการตรวจสอบ |
| ย้อนกลับ | git checkout <commit> -- file |
เมื่อมีปัญหา |
| Push ขึ้น Remote | git push |
สิ้นวัน หรือหลัง Commit สำคัญ |
| Pull ก่อนเริ่มงาน | git pull |
ทุกเช้า |
Commit Message ที่ดีสำหรับ IT
| ดี | ไม่ดี |
|---|---|
| “Add SSL cert for api.company.com” | “update” |
| “Fix nginx 502 by increasing proxy_read_timeout” | “fix bug” |
| “Block port 22 from external in firewall” | “change firewall” |
| “Update PHP to 8.3 in docker-compose” | “แก้ config” |
Next Steps — เรียนอะไรต่อ?
- Branching Strategies: Git Flow, GitHub Flow, Trunk-Based Development
- Pull Request (PR): วิธี Code Review ก่อน Merge
- Git Hooks: รัน Script อัตโนมัติก่อน/หลัง Commit
- CI/CD Integration: เชื่อม Git กับ Jenkins, GitLab CI, GitHub Actions
- Infrastructure as Code: ใช้ Git จัดการ Terraform, Ansible Playbooks
สรุป: Git = สิ่งที่ IT ทุกคนต้องรู้
Git ไม่ใช่เครื่องมือสำหรับ Developer เท่านั้น IT ที่ดูแลระบบ จัดการ Config เขียน Script ทุกคนได้ประโยชน์จาก Git เริ่มวันนี้ด้วย 5 คำสั่ง: git init, git add, git commit, git push, git pull ทำซ้ำทุกวันจนเป็นนิสัย แล้วคุณจะไม่กลับไปจัดการไฟล์แบบเดิมอีกเลย