DEV Community

Lumin
Lumin

Posted on

1 1

ทำงานเร็วขึ้นด้วย 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 
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

  1. ทำ shell alias โดยใช้ alias
alias gc='git commit'
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay