เชี่ยวชาญ Git ใน 30 วัน

วันที่ 30: แบ่งปันเคล็ดลับการใช้งาน Git ที่มีประโยชน์จากการทำงานจริง

หลังจากเรียนรู้ Git มา 29 วัน บทความสุดท้ายนี้จะแบ่งปันเคล็ดลับและเทคนิคที่ใช้งานได้จริง ซึ่งทำให้การใช้งาน Git ในชีวิตประจำวันมีประสิทธิภาพและเพลิดเพลินยิ่งขึ้น

เคล็ดลับการกำหนดค่า

การกำหนดค่าส่วนกลาง

# Set default editor
git config --global core.editor "code --wait"

# Enable color UI
git config --global color.ui auto

# Set default branch name
git config --global init.defaultBranch main

# Auto-correct typos
git config --global help.autocorrect 1

# Cache credentials
git config --global credential.helper cache --timeout=3600

Alias ที่มีประโยชน์

# Short status
git config --global alias.st status

# Short log
git config --global alias.ll "log --oneline --graph --all --decorate"

# Last commit
git config --global alias.last "log -1 HEAD"

# Undo last commit (keep changes)
git config --global alias.undo "reset HEAD~1 --mixed"

# Amend without editing message
git config --global alias.amend "commit --amend --no-edit"

# List branches sorted by last modified
git config --global alias.br "branch --sort=-committerdate"

# Show changed files in last commit
git config --global alias.dl "diff --name-only HEAD~1"

# Search commit messages
git config --global alias.search "log --all --grep"

# Nuke branch (force delete)
git config --global alias.nuke "branch -D"

เคล็ดลับเพิ่มประสิทธิภาพ

การนำทางอย่างรวดเร็ว

# Jump to repository root
alias gr='cd $(git rev-parse --show-toplevel)'

# Open repo in browser
alias browse='git remote get-url origin | xargs open'

การค้นหา

# Find file in history
git log --all --full-history -- "**/filename.*"

# Search in all commits
git log -S "search term" --source --all

# Find who changed a line
git blame filename

# Prettier blame
git blame -w -C -C -L 10,20 filename

การเก็บกวาด

# Delete merged branches
git branch --merged main | grep -v "^*\|main" | xargs git branch -d

# Delete all local branches except main
git branch | grep -v "main" | xargs git branch -D

# Remove untracked files (dry run first!)
git clean -n
git clean -fd

# Prune remote branches
git fetch --prune

คำสั่งประหยัดเวลา

Commit อย่างรวดเร็ว

# Add and commit in one step
git commit -am "Quick fix"

# Add, commit, and push
alias gcp='git commit -am "$1" && git push'

การจัดการ Branch

# Create and checkout branch in one step
git checkout -b feature/new-feature

# Switch to previous branch
git checkout -

# Show branches with last commit
git branch -v

# Show remote tracking branches
git branch -vv

เทคนิค Stash

# Stash with message
git stash push -m "Work in progress on feature X"

# List stashes with details
git stash list --pretty=format:"%C(yellow)%gd%C(reset) - %C(bold)%s%C(reset)"

# Apply and drop in one step
git stash pop

# Apply specific stash
git stash apply stash@{2}

# Stash including untracked files
git stash -u

เทคนิคขั้นสูง

Cherry-Pick หลาย Commit

# Cherry-pick a range of commits
git cherry-pick A^..B

# Cherry-pick from another branch
git cherry-pick feature-branch~3..feature-branch

Interactive Staging

# Stage parts of a file
git add -p filename

# Interactive add
git add -i

เคล็ดลับ Diff

# Word diff instead of line diff
git diff --word-diff

# Ignore whitespace changes
git diff -w

# Show diff of staged changes
git diff --staged

# Diff between branches
git diff main..feature-branch

# Diff specific file between commits
git diff commit1 commit2 -- filename

การจัดรูปแบบ Log

# Pretty log
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)(%cr)%C(reset) %s %C(blue)<%an>%C(reset)"

# Log with graph
git log --graph --abbrev-commit --decorate --all

# Show files changed per commit
git log --stat

# Show patches
git log -p

การแก้ไขปัญหา

ยกเลิกข้อผิดพลาด

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo all uncommitted changes
git checkout .

# Unstage file
git reset HEAD filename

# Restore file from HEAD
git checkout HEAD -- filename

การกู้คืน

# Find lost commits
git reflog

# Recover deleted branch
git branch branch-name <commit-hash>

# Find dangling commits
git fsck --lost-found

การ Debug

# See who introduced a bug (binary search)
git bisect start
git bisect bad  # Current version is bad
git bisect good <commit>  # Known good commit
# Git checks out middle commit
# Test and mark as good or bad
git bisect good/bad
# Repeat until found
git bisect reset

# Blame with line range
git blame -L 10,20 filename

# Show commit that introduced line
git log -S "function name" --source --all

การปรับ Workflow ให้เหมาะสม

Pre-Commit Hooks

สร้าง .git/hooks/pre-commit:

#!/bin/bash

# Run tests before commit
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed, commit aborted"
    exit 1
fi

เทมเพลตข้อความ Commit

สร้าง ~/.git-commit-template:

# <type>: <subject>
#
# <body>
#
# <footer>
#
# Types: feat, fix, docs, style, refactor, test, chore

ตั้งค่าเป็นค่าเริ่มต้น:

git config --global commit.template ~/.git-commit-template

เคล็ดลับการผสานรวม

Git กับ VS Code

# Open current repo in VS Code
code .

# Open merge tool
git mergetool --tool=vscode

Git กับ Vim

# Set Vim as default editor
git config --global core.editor vim

# Vim as diff tool
git config --global diff.tool vimdiff
git config --global difftool.prompt false

เคล็ดลับด้านประสิทธิภาพ

Repository ขนาดใหญ่

# Shallow clone (faster)
git clone --depth 1 https://github.com/user/repo.git

# Partial clone (Git 2.22+)
git clone --filter=blob:none https://github.com/user/repo.git

# Sparse checkout (specific folders)
git sparse-checkout init
git sparse-checkout set folder1 folder2

Status ที่เร็วขึ้น

# Use untracked cache
git config core.untrackedCache true

# Use file system monitor
git config core.fsmonitor true

เคล็ดลับด้านความปลอดภัย

การเซ็นชื่อด้วย GPG

# Generate GPG key
gpg --gen-key

# List keys
gpg --list-secret-keys --keyid-format LONG

# Configure Git
git config --global user.signingkey <key-id>
git config --global commit.gpgsign true

# Sign commits
git commit -S -m "Signed commit"

ข้อมูลประจำตัว

# Never store passwords in config
# Use credential helpers instead

# SSH for authentication (recommended)
ssh-keygen -t ed25519 -C "email@example.com"

# Use HTTPS with token
# Generate personal access token on GitHub
# Use it as password

ปรัชญาสุดท้าย

รายการตรวจสอบแนวปฏิบัติที่ดีที่สุด

  • Commit บ่อย ๆ, Push เป็นประจำ
  • เขียนข้อความ Commit ที่มีความหมาย
  • ทำให้ Commit เป็นหน่วยเดียวและมีจุดเน้น
  • สร้าง Branch สำหรับทุก Feature/การแก้ไข
  • Review โค้ดของคุณเองก่อนสร้าง PR
  • Pull ก่อน Push
  • ห้าม Force Push ไปยัง Branch ที่แชร์กันเด็ดขาด
  • ใช้ .gitignore อย่างถูกต้อง
  • Tag Release
  • บันทึก Workflow ของคุณเป็นเอกสาร

เมื่อเกิดข้อผิดพลาด

  1. อย่าตื่นตระหนก: Git แทบไม่เคยทำข้อมูลสูญหาย
  2. ตรวจสอบ reflog: มันคือตาข่ายนิรภัยของคุณ
  3. สำรองข้อมูล: สร้าง Branch ก่อนทดลอง
  4. ขอความช่วยเหลือ: ชุมชน Git พร้อมช่วยเหลือ
  5. เรียนรู้จากข้อผิดพลาด: ทุกข้อผิดพลาดสอนบางสิ่งให้คุณ

ขอแสดงความยินดี!

คุณเรียนจบ 30 วันแห่งการเชี่ยวชาญ Git แล้ว! คุณได้เรียนรู้:

  • พื้นฐานและสถาปัตยกรรมของ Git
  • กลยุทธ์ Branch และ Merge
  • การจัดการประวัติ (reset, rebase, amend)
  • Workflow แบบ Remote และการทำงานร่วมกัน
  • Workflow และแนวปฏิบัติที่ดีที่สุดของ GitHub
  • เคล็ดลับขั้นสูงและการแก้ไขปัญหา

เรียนรู้ต่อไป

  • ฝึกฝนเป็นประจำ
  • มีส่วนร่วมใน Open Source
  • เรียนรู้จากประวัติ Git ของผู้อื่น
  • สำรวจคุณสมบัติขั้นสูง
  • ติดตาม Release ของ Git อยู่เสมอ

ความคิดสุดท้าย

Git เป็นเครื่องมือที่ทรงพลัง ซึ่งยิ่งคุณใช้มากเท่าใดก็ยิ่งมีคุณค่ามากขึ้นเท่านั้น อย่าพยายามจำทุกอย่าง บุ๊กมาร์กคู่มือนี้ไว้และกลับมาอ้างอิงเมื่อจำเป็น สิ่งที่สำคัญที่สุดคือฝึกฝนต่อไป และอย่ากลัวที่จะทดลอง (ในสภาพแวดล้อมที่ปลอดภัย)

ขอให้เขียนโค้ดอย่างมีความสุข และขอให้การ Merge ของคุณปราศจาก Conflict เสมอ!

ลิงก์อ้างอิง



ขอบคุณ!

ขอบคุณที่ติดตามการเดินทางเรียนรู้ Git ตลอด 30 วันนี้ ฝึกฝนต่อไป เรียนรู้ต่อไป และจำไว้ว่า ผู้เชี่ยวชาญ Git ทุกคนก็เคยเป็นมือใหม่มาก่อน!