Thanks to visit codestin.com
Credit goes to dev.to

DEV Community

Lumin
Lumin

Posted on

ทำงานเร็วขึ้นด้วย git alias

ผมยังเห็นน้องๆในที่ทำงานยังใช้ SourceTree สำหรับทำงานกับ git อยู่
ในใจรู้สึกอึดอัดมากๆ เพราะผมไม่สามารถใช้เครื่องมือพวกนั้นได้
เครื่อง Mac Air มันช้าและ mem น้อยจนเกินกว่าที่รันของพรรค์นั้นไหว

สำหรับผมแล้วรัน git command ใน terminal นี่แหละสะดวกและประหยัดพลังงานที่สุด

แต่การจะต้องพิมพิ์คำสั่งยาวๆมันก็ไม่ไหว มันไม่คูลล์
ผมจะเล่าให้ฟัง ว่าคูลล์ๆเค้าทำยังงัย

ตามปกติ เราทำงานกับ git จะต้องทำตามขั้นตอนประจำ add/commit/push

# เพิ่มไฟล์ทั้งหมดที่มีการเปลี่ยนแปลงลง stage 
$ git add .

# สั่ง commit code และใส่ message ไปด้วย 
$ git commit -m "COMMIT MESSAGES"

# สั่ง push code ขึ้น origin 
$ git push 
Codestin Search App Codestin Search App

จะเห้นว่าเราต้องพิมพิ์ซ้ำเยอะ
เราสามารถทำ short cut ได้ 2 วิธี เพื่อลดจำนวนอักษรที่ต้องพิมพิ์

  1. สั่ง config ผ่าน git-config
# สร้าง short cut ให้ c = commit 
$ git config --global alias.c commit 

# เวลาใช้งาน ก็ใช้ c แทน commit ไปเลย
$ git c -m "COMMIT MESSAGES"
Codestin Search App Codestin Search App
  1. แก้ไขไฟล์​ ~/.gitconfig
[alias]
  c = commit
Codestin Search App Codestin Search App

ซึ่งแบบที่ 1 และ 2 ถือว่ายังไม่ค่อยโอเค เพราะต้องใช้ git ตลอด
แบบที่เวิร์คที่สุดคือ

  1. ทำ shell alias โดยใช้ alias
alias gc='git commit'
Codestin Search App Codestin Search App

แบบนี้เวลาใช้งานเราก็แค่รัน gc -m "COMMIT MESSAGE"

หรือเข้าไปแก้ไข ~/.bashrc โดยเพิ่ม function แบบนี้เหมาะสำหรับเราจะเพิ่ม logic เทพๆเข้าไป โดยที่เราไม่ต้องใส่ option ที่ต้องใส่ซ้ำๆได้

function gcm() {
  # ใน bash $1 หมายถึง argument ตัวแรก
  # สำหรับ function นี้เวลาใช้งานคือ
  # $ gcm "COMMIT MESSAGE"
  # จะเห็นว่า $1 = "COMMIT MESSAGE" นั่นเอง
  git commit -m "$1"
}
Codestin Search App Codestin Search App

แล้วพบกันใหม่ สวัสดี

Top comments (0)