# 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. ตรวจสอบสถานะไฟล์
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
# ดูไฟล์ที่เปลี่ยนแปลง
git status
# ยกเลิกการแก้ไขไฟล์เดียว
git checkout -- app.js
# หรือยกเลิกทั้งหมด
git checkout -- .
# Git 2.23+: ใช้คำสั่งใหม่
git restore app.js
# ยกเลิกการ add ไฟล์เดียว
git reset HEAD config.js
# ยกเลิกทั้งหมด
git reset HEAD .
# Git 2.23+: ใช้คำสั่งใหม่
git restore --staged config.js
# ดู 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
# กลับไปที่ main branch
git checkout main
# รวม feature branch เข้ามา
git merge feature/user-profile
# ดู merge ที่เกิดขึ้น
git log --oneline --graph
# ลบ branch ที่ไม่ใช้แล้ว
git branch -d feature/user-profile
# เพิ่ม 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
# 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
# สร้าง 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
# ดู 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
# เก็บการเปลี่ยนแปลง
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}
# 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
# 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
# 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
# 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
main: Production codedevelop: Development integrationfeature/*: New featuresrelease/*: Release preparationhotfix/*: Production fixes# 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
# 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
# สร้าง 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
# ดู 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}
# 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
# 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
<type>(<scope>): <subject>
<body>
<footer>
Types:
feat: Feature ใหม่fix: แก้ bugdocs: เอกสารstyle: Format, ไม่เปลี่ยนโค้ดrefactor: Refactor โค้ดtest: เพิ่ม testschore: งาน maintenancefeature/description # Feature ใหม่
bugfix/description # แก้ bug
hotfix/description # แก้ bug ด่วน
release/version # เตรียม release