Git
Version control — branching strategy, code review, CI/CD workflow
What I Can Do
- จัดการ branching strategy (Git Flow, GitHub Flow, Trunk-Based)
- ใช้ interactive rebase เพื่อ clean up commit history
- แก้ merge conflicts อย่างถูกต้องและรวดเร็ว
- กู้คืน lost commits ด้วย reflog
- ใช้ bisect หา commit ที่ทำให้เกิด bug
Commands I Use Daily
# ดูสถานะไฟล์ที่เปลี่ยน
git status
# stage เฉพาะ hunks ที่ต้องการ (ไม่ใช่ทั้งไฟล์)
git add -p
# ดู commit history แบบ graph
git log --oneline --graph --all
# interactive rebase — squash, reorder, edit commits
git rebase -i HEAD~5
# เก็บงานชั่วคราว
git stash
git stash pop
# ดู diff ก่อน commit
git diff --staged
# กู้คืน — ดู history ของ HEAD movements
git reflogRepository & Working Directory
Git repository = .git directory ที่เก็บ history ทั้งหมด — working directory คือ files ที่เห็นและแก้ไข, git init สร้าง repo ใหม่, git clone copy จาก remote
Staging Area (Index)
Staging area (index) อยู่ระหว่าง working directory กับ repository — git add เลือก changes ที่จะรวมใน commit ถัดไป ทำให้ commit ได้เฉพาะ changes ที่เกี่ยวข้อง ไม่ต้อง commit ทุกอย่างพร้อมกัน
Commit Basics
Commit = snapshot ของ staged changes + message อธิบายว่าเปลี่ยนอะไรทำไม — แต่ละ commit มี unique SHA hash, ชี้ไปหา parent commit(s) ทำให้เกิด chain of history
Branching & Merging
- Branch — pointer ไปยัง commit, สร้างถูก (แค่ 41 bytes),
git branch,git checkout -b - Merge — รวม branch เข้าด้วยกัน, fast-forward (ถ้า linear) หรือ merge commit (ถ้า diverged)
- Feature branches แยก work ไม่กระทบ main branch
Remote (Push/Pull/Fetch)
git remote— จัดการ connections ไปยัง remote repositories (origin, upstream)git fetch— download changes จาก remote โดยไม่ mergegit pull— fetch + merge (หรือ--rebaseเพื่อ rebase แทน merge)git push— upload commits ไปยัง remote
.gitignore
ไฟล์ .gitignore กำหนด patterns ของไฟล์ที่ Git จะไม่ track — node_modules/, .env, *.log, build artifacts ใส่ไว้ root ของ project, สามารถมี .gitignore ใน subdirectories ได้
Diff & Log
git diff— ดู unstaged changes,--stagedดู staged changes,branch1..branch2เทียบ branchesgit log— ดู commit history,--onelineสั้นๆ,--graphเห็น branching,-pดู diff ของแต่ละ commit,--authorfilter by author
Stash
git stash เก็บ uncommitted changes ชั่วคราว — ใช้เมื่อต้อง switch branch แต่ยังไม่อยาก commit, git stash pop เอากลับมา, git stash list ดูรายการ, git stash apply stash@{n} เลือก stash ที่ต้องการ
Rebasing
git rebase main — ย้าย commits ของ branch ปัจจุบันไปต่อจาก tip ของ main — ได้ linear history ที่อ่านง่าย ข้อควรระวัง: ไม่ rebase commits ที่ push ไปแล้ว (shared history)
Interactive Rebase
git rebase -i HEAD~N — เลือก squash, reword, edit, drop, reorder commits ใช้ clean up history ก่อน merge — squash WIP commits, แก้ typo ใน commit message, แยก commit ใหญ่เป็นชิ้นเล็ก
Cherry-pick
git cherry-pick <sha> — copy commit เดียวจาก branch อื่นมาใส่ branch ปัจจุบัน ใช้เมื่อต้องการ specific fix โดยไม่ merge ทั้ง branch เช่น hotfix จาก develop ไป main
Merge Strategies
- Fast-forward — ย้าย pointer ไปข้างหน้า (ไม่สร้าง merge commit)
- Recursive/ort — default strategy, handle diverged branches
- Squash merge —
git merge --squashรวมทุก commits เป็น commit เดียว, clean history - No-ff —
git merge --no-ffบังคับสร้าง merge commit เสมอ
Tags
- Lightweight tag — pointer ไปยัง commit (
git tag v1.0) - Annotated tag — มี message, author, date (
git tag -a v1.0 -m "Release 1.0") - ใช้ mark releases,
git push --tagspush ไป remote
Git Hooks
Scripts ที่ run อัตโนมัติเมื่อเกิด events — pre-commit (lint/format ก่อน commit), commit-msg (validate message format), pre-push (run tests ก่อน push) อยู่ใน .git/hooks/, ใช้ tools เช่น husky จัดการ
Reflog (Recovery)
git reflog แสดง history ของ HEAD movements ทุกครั้ง — ใช้กู้คืน commits ที่ "หาย" จาก reset, rebase ผิด, deleted branch ค้นหา SHA แล้ว git checkout <sha> หรือ git reset --hard <sha>
Bisect (Debugging)
git bisect start → git bisect bad → git bisect good <sha> — Git ใช้ binary search หา commit ที่ introduce bug, ลดจำนวน commits ที่ต้องตรวจจาก N เหลือ log2(N) ใช้ git bisect run <test> automate ได้
Worktrees
git worktree add ../feature-branch feature-branch — สร้าง working directory เพิ่มจาก repo เดียวกัน ใช้ทำงานหลาย branches พร้อมกันโดยไม่ต้อง stash/switch เช่น review PR ขณะยัง develop feature อยู่
Subtrees & Submodules
- Submodule — link ไปยัง specific commit ของ repo อื่น,
git submodule add - Subtree — copy code จาก repo อื่นเข้ามาใน project,
git subtree add - Subtree ง่ายกว่า ไม่ต้อง init/update แยก แต่ history ปนกัน
Large File Handling (Git LFS)
Git LFS เก็บ large files (images, videos, models) บน separate storage แทนที่จะ track ใน repo โดยตรง — git lfs track "*.psd", ลดขนาด repo, clone เร็วขึ้น
Monorepo Strategies
จัดการ multiple projects ใน repo เดียว — ใช้ tools เช่น Turborepo, Nx สำหรับ selective builds/tests, path-based CODEOWNERS สำหรับ review, sparse checkout สำหรับ clone เฉพาะ directory ที่ต้องการ