วิธีเขียน Git Alias เพื่อเพิ่มความเร็วในการทำงาน

Git Alias เป็นฟีเจอร์ที่ช่วยให้คุณสามารถสร้างคำสั่งย่อสำหรับ Git Command ที่ยาวหรือใช้บ่อย เพื่อเพิ่มความเร็วและลดการพิมพ์ในการทำงานวันๆ ของผู้พัฒนา เมื่อคุณใช้ Alias หรือคำสั่งย่อ คุณสามารถประหยัด Keystroke ได้มากมาย ทำให้ Development Workflow มีประสิทธิภาพสูงขึ้น โดยเฉพาะเมื่อทำงานบน VPS ของ ผู้ให้บริการโฮสติ้ง

Git Alias คืออะไร

Git Alias เป็นวิธีการสร้างชื่อสั้นหรือตัวอักษรย่อสำหรับคำสั่ง Git ที่ยาวๆ ตัวอย่างเช่น แทนที่จะพิมพ์ “git status” ทุกครั้ง คุณสามารถสร้าง Alias ว่า “git st” ก็พอ Git Alias ช่วยให้การทำงาน Git มีประสิทธิภาพมากขึ้น เพราะคุณไม่ต้องพิมพ์คำสั่งทั้งหมด ทำให้ลดข้อผิดพลาดจากการพิมพ์ผิด และประหยัดเวลา ความจำ

Git Alias ประกอบด้วย 2 ประเภท คือ Alias ของคำสั่ง Git เอง (git aliases) และ Alias ของคำสั่ง Shell (shell aliases) ส่วนใหญ่เราจะใช้ git aliases ที่เก็บไว้ในไฟล์ ~/.gitconfig เพื่อให้ใช้ได้ทุก Repository บนเครื่องเรา

ข้อดีของ Git Alias

  • ลดจำนวน Keystroke ในการพิมพ์คำสั่ง ทำให้การทำงานเร็วขึ้น
  • หลีกเลี่ยง Typo และข้อผิดพลาดจากการพิมพ์ผิด
  • ลดความเหนื่อยจากการพิมพ์คำสั่งที่ยาวและซ้ำๆ
  • สร้างความสะดวกและลักษณะการทำงาน (workflow) ที่ตรงกับสไตล์ของคุณ
  • ทำให้ชีวิต Developer ง่ายขึ้น ลดความเครียดจากการใช้ Command Line
  • เพิ่มความมั่นใจในการใช้ Git
  • ช่วยให้ทีมสามารถมี Standard Alias ที่เหมือนกัน

วิธีตั้ง Git Alias พื้นฐาน

วิธีการตั้ง Alias มีหลายวิธี ที่ง่ายที่สุดคือใช้คำสั่ง git config ต่อไปนี้

# Alias พื้นฐาน - เพื่อลดการพิมพ์
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.aa 'add -A'

# Alias สำหรับการดู Log
git config --global alias.lo 'log --oneline'
git config --global alias.lg 'log --graph --pretty=format:"%h - %an, %ar : %s"'

# Alias สำหรับการทำงาน
git config --global alias.unstage 'reset HEAD --'
git config --global alias.undo 'reset --soft HEAD~1'

ตัวเลือก “–global” ทำให้ Alias สามารถใช้ได้ในทุก Repository บนเครื่องของคุณ หากต้องการใช้เฉพาะ Repository นั้นๆ ให้ใส่ “–local” แทน

Git Alias ที่ใช้บ่อยและมีประโยชน์

นี่คือรายการ Alias ที่นิยมใช้บ่อยเพื่อเพิ่มประสิทธิภาพการทำงาน Git

# Status สั้น
git config --global alias.st status
git config --global alias.ss 'status -s'    # Very short status

# Branch Operations
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.cb 'checkout -b'  # Create new branch
git config --global alias.db 'branch -d'    # Delete branch

# Commit Operations
git config --global alias.cm commit
git config --global alias.ca 'commit --amend'
git config --global alias.c 'commit -m'
git config --global alias.cf 'commit --fixup'

# Add and Remove
git config --global alias.aa 'add -A'
git config --global alias.ap 'add -p'       # Add patch
git config --global alias.rm 'remove'

# Push and Pull
git config --global alias.ps push
git config --global alias.pl pull
git config --global alias.pf 'push --force-with-lease'

# Diff
git config --global alias.df diff
git config --global alias.dc 'diff --cached'

# Log
git config --global alias.lo 'log --oneline'
git config --global alias.l 'log --all --decorate --oneline --graph'
git config --global alias.last 'log -1 HEAD'
git config --global alias.recent 'log --all --oneline -20'

# Merge and Rebase
git config --global alias.mg merge
git config --global alias.rb rebase
git config --global alias.rbi 'rebase -i'

# Reset
git config --global alias.rs 'reset --hard'
git config --global alias.us 'reset HEAD'
git config --global alias.undo 'reset --soft HEAD~1'

Alias ขั้นสูงและ Compound Commands

นอกจากการสร้าง Alias อย่างง่ายแล้ว คุณยังสามารถสร้าง Alias ที่รวมหลาย Commands เข้าด้วยกันได้ เหล่านี้เรียกว่า Compound Commands หรือ Shell Commands

# Alias สำหรับแสดงรายชื่อ Alias ทั้งหมด
git config --global alias.aliases 'config --get-regexp alias'

# Alias สำหรับดู Branches ที่มีอยู่ (รวม Remote)
git config --global alias.branches 'branch -a'

# Alias สำหรับดู Remote URLs
git config --global alias.remote-url 'config --get remote.origin.url'

# Alias สำหรับลบ Branches ที่ไม่ได้ใช้แล้ว
git config --global alias.clean 'branch -vv | grep ": gone\]" | awk '"'"'{print $1}'"'"' | xargs -r git branch -d'

# Alias สำหรับดู Commits ที่ยังไม่ได้ Push
git config --global alias.unpushed 'log @{u}..'

# Alias สำหรับ Stash
git config --global alias.sh stash
git config --global alias.shp 'stash pop'

# Alias สำหรับดู Tag
git config --global alias.tags 'tag -l'

# Alias สำหรับการทำความสะอาด Repository
git config --global alias.tidy 'clean -fd'
git config --global alias.gc 'gc --aggressive'

# Alias สำหรับ Feature Branch
git config --global alias.feature 'checkout -b feature/'
git config --global alias.bugfix 'checkout -b bugfix/'
git config --global alias.hotfix 'checkout -b hotfix/'

การแก้ไข Git Alias โดยตรงแก้ไขไฟล์ ~/.gitconfig

หากต้องการเพิ่ม Alias หลายตัวพร้อมกัน คุณสามารถแก้ไขไฟล์ ~/.gitconfig ได้โดยตรง

# เปิดไฟล์ .gitconfig ด้วย Editor ที่คุณใช้
nano ~/.gitconfig
# หรือ vim ~/.gitconfig

# หา Section [alias] หรือสร้างใหม่ แล้วเพิ่ม Alias ดังนี้
[alias]
    st = status
    ss = status -s
    co = checkout
    cb = checkout -b
    br = branch
    cm = commit
    ca = commit --amend
    aa = add -A
    ap = add -p
    ps = push
    pl = pull
    pf = push --force-with-lease
    df = diff
    dc = diff --cached
    lo = log --oneline
    l = log --all --decorate --oneline --graph
    last = log -1 HEAD
    mg = merge
    rb = rebase
    rbi = rebase -i
    rs = reset --hard
    us = reset HEAD
    undo = reset --soft HEAD~1
    unstage = reset HEAD --
    aliases = config --get-regexp alias
    branches = branch -a
    unpushed = log @{u}..
    sh = stash
    shp = stash pop
    feature = checkout -b feature/
    bugfix = checkout -b bugfix/

หลังจากบันทึกไฟล์ (กด Ctrl+X แล้ว Y และ Enter สำหรับ nano หรือ :wq! สำหรับ vim) Alias ทั้งหมดจะเข้าใช้งานทันที

Global Alias vs Local Alias – เข้าใจความแตกต่าง

Git Alias มี 2 ลักษณะ คือ Global และ Local

  • Global Alias – ใช้ได้ทั่วทั้ง Repository บนเครื่องคุณ เก็บไว้ในไฟล์ ~/.gitconfig
  • Local Alias – ใช้ได้เฉพาะ Repository นั้นๆ เท่านั้น เก็บไว้ในไฟล์ .git/config ของ Repository นั้น
# สร้าง Global Alias
git config --global alias.st status

# สร้าง Local Alias (ต้องอยู่ใน Repository)
git config alias.project-alias 'log --all'

# ดูเฉพาะ Local Alias
git config --list --local | grep alias

# Local Alias สามารถ Override Global Alias ได้
# ถ้าตั้ง Local Alias เดียวกับ Global จะใช้ Local ก่อน

Tips สำหรับการใช้ Git Alias อย่างมีประสิทธิภาพ

  • เลือกชื่อ Alias ที่ง่ายจำ โดยปกติ 1-3 ตัวอักษร เช่น “st” แทน “status”
  • ใช้ Global Alias สำหรับคำสั่งทั่วไป และ Local Alias สำหรับคำสั่งที่เฉพาะ Project
  • สร้าง Alias ของ Alias เพื่อลด Keystroke ลงไปอีก แต่อย่าให้ซับซ้อนเกินไป
  • Backup ไฟล์ ~/.gitconfig เพื่อสถานที่อื่นเพื่อความปลอดภัย
  • ใช้คำสั่ง “git aliases” เพื่อดู Alias ทั้งหมดที่มีอยู่
  • อย่าสร้าง Alias ที่ขัดแย้งกับ Git Built-in Commands
  • แชร์ Alias ที่ดีกับทีมเพื่อลดความแตกต่างในวิธีการทำงาน
  • ตรวจสอบการใช้งาน Alias บ่อยๆ และปรับปรุง Alias ที่ไม่ค่อยใช้
# Alias สำหรับ Production Deployment
git config --global alias.prod-update 'pull origin production && git log --oneline -5'
git config --global alias.prod-status 'status'

# Alias สำหรับ Staging
git config --global alias.staging-sync 'checkout staging && git pull origin staging'

# Alias สำหรับ Development
git config --global alias.dev-sync 'checkout develop && git pull origin develop'

# Alias สำหรับ Quick Fixes
git config --global alias.quick-fix '!git checkout -b fix/ && git add -A && git commit'

# Alias สำหรับ Code Review
git config --global alias.review '!git diff'

# Alias สำหรับ Deploy
git config --global alias.deploy '!git push origin $(git rev-parse --abbrev-ref HEAD)'

การ Troubleshoot และ Debug Alias

หากเจอปัญหากับ Alias คุณสามารถดำเนินการตรวจสอบได้ดังนี้

# แสดงรายชื่อ Alias ทั้งหมด
git aliases

# ตรวจสอบว่า Alias ใดทำให้เกิดข้อผิดพลาด
git st -v

# ดูรายละเอียดของคำสั่ง Alias
git config --get alias.st

# ลบ Alias ที่ไม่ต้องการ
git config --global --unset alias.st

# ดู Full Config
git config --list --global | grep alias

Shell Alias vs Git Alias – เมื่อไหร่ใช้อันไหน

บางคนอาจจะสร้าง Shell Alias ในไฟล์ ~/.bashrc หรือ ~/.zshrc แทน ทั้ง Shell Alias และ Git Alias ก็มีข้อดีแตกต่างกัน Git Alias ดีกว่าสำหรับคำสั่ง Git เพราะมันจะไม่ทำให้ Shell ของคุณเต็มไป และใช้ได้ทุกเครื่องที่คุณ Clone Repository Shell Alias ดีกว่าสำหรับคำสั่ง Shell หรือ Script ที่ไม่เกี่ยวข้องกับ Git

# Shell Alias (ในไฟล์ ~/.bashrc)
alias ll='ls -la'
alias gst='git status'

# Git Alias (ดีกว่า)
git config --global alias.st status

# ข้อแตกต่าง: Shell Alias ต้องพิมพ์ "gst" แต่ Git Alias ต้องพิมพ์ "git st"
# ข้อดี Git Alias: ใช้ได้ทุกการ Clone Repository ใหม่ เพราะเก็บใน ~/.gitconfig

สรุป

Git Alias เป็นเครื่องมือที่ง่ายแต่มีประสิทธิภาพสูงสำหรับเพิ่มความเร็วในการทำงาน Git เมื่อคุณสร้าง Alias ที่เหมาะสม คุณจะพบว่า Development Workflow ของคุณมีประสิทธิภาพมากขึ้น ลดข้อผิดพลาด และหลีกเลี่ยง Typo ได้อย่างดี ลองสร้าง Alias ตามที่แนะนำ และปรับเปลี่ยนให้เหมาะกับสไตล์การทำงานของคุณ ผู้พัฒนาบน VPS ของ ผู้ให้บริการโฮสติ้ง จะพบว่าการใช้ Git Alias ช่วยให้ Development มีประสิทธิภาพและสะดวกสบายมากขึ้น