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

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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay