โพยการใช้ git เมื่อทำ side project บน Github

Programming May 16, 2025

เราจะใช้ Github เมื่อทำ site project ต่าง ๆ ดังนั้นเลยใช้ command line หรือ git บน Android Studio หรือ VS Code เป็นหลัก เลยมาทำโพยกันลืมสักหน่อย

เปลี่ยน email

บางคนเครื่องนึงอาจจะใช้หลาย account เพื่อความมั่นใจ ก็สลับ email account เดียวกับที่ใช้ใน Github เพื่อหลังจาก push code จะได้ขึ้นชื่อ Github ของเราอย่างสวยงาม

git config user.email <your email>

สร้าง repository ใหม่จาก command line เพื่อชี้ไป remote บน Github

ในกรณีที่เราสร้าง repository ใหม่เลยจาก Github เลย แบบยังไม่มีอะไรเลยใน project เขาจะแนะนำให้เราสร้างไฟล์ README.md โดยการเขียน text ลงไปในไฟล์ echo "# <repo name>" >> README.md เข้าไป

ต่อมาสร้าง local repository ใหม่ ที่ directory นี้ด้วยคำสั่ง git init

แล้วเพิ่มไฟล์ใหม่เข้าไปใน repository ด้วย git add <file name> ในที่นี้คือ git add README.md หรือเราจะเพิ่มทุก file ใน directory นี้ได้ด้วย git add .

จากนั้น commit file ทั้งหมดที่เราเพิ่มไป พร้อมทั้ง commit message ด้วย git commit -m "first commit"

อย่าลืมสร้าง local branch ด้วยคำสั่ง git branch -M <branch name>

👨‍🏫
แล้ว -M คืออะไร? มันย่อจาก --move --force เป็นการ rename branch ล่าสุด คือ master เป็น main เพราะประเด็นเรื่องความหมายที่ไม่เหมาะสมกับบางบริบท ซึ่งมีความเชื่อมโยงกับคำว่า master (นาย) และ slave (ทาส) เลยทำให้บางคนไม่สบายใจ เลยเปลี่ยนชื่อเป็น main นั่นเอง และยังตรงบริบทกว่าด้วย

แล้วก็เพิ่ม remote repository ในเครื่องด้วยคำสั่ง git remote add <remote name> <url> โดยชื่อ remote มักใช้ชื่อ origin

สุดท้าย push code ไปที่ Github โดยใช้คำสั่ง git push -u <remote name> <remote branch> ซึ่งควรจะตั้งชื่อ branch ให้ตรงกันทั้งบน local และ remote นะ ในที่นี้ชื่อ main เนอะ

echo "# <repo name>" >> README.md
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <https://www.github.com/><username>/<repo name>.git
git push -u origin main

เมื่อเราทำงานแล้วอยากจะ push บน Github

ในเคสที่เราทำ site project ไปสักพัก แล้วอยากอัพขึ้นบน Github แน่นอนว่าจะไม่ใช่ directory เปล่า ๆ ดังนั้นขั้นตอนจะสลับจากเมื่อกี้นิดนึง เริ่มจาก สร้าง local repository ใหม่ ที่ directory ที่เราทำงาน

เอ้อถ้า directory นั้นยังไม่ได้ใช้ git อย่าลืมใช้ git init ก่อนนะ

หลังจากที่เราสร้าง repository บน Github แล้ว มาเพิ่ม remote repository ในเครื่องเรากันก่อน จากนั้นสร้าง branch ใหม่ใน local กันก่อน และ push code ไปที่ Github

git remote add origin <https://www.github.com/><username>/<repo name>.git
git branch -M main
git push -u origin origin/main

สำหรับคำสั่ง git command พื้นฐาน สามารถอ่านได้ที่บล็อก cheatsheet นี้เลย 👇

สรุป Git command เผื่อได้ใช้กัน
เราสรุป Git command ที่ได้จากการเรียนคอร์สออนไลน์ที่ชื่อว่า Git Essentials for Developers ของทาง Skooldio เลยทำ cheatsheet นี้ขึ้นมา เผื่อเพื่อน ๆ ได้เอามาใช้กันเนอะ
https://www.mikkipastel.com/git-command-summary/

ติดตามข่าวสารตามช่องทางต่าง ๆ และทุกช่องทางโดเนทกันไว้ที่นี่เลย แนะนำให้ใช้ tipme เน้อ ผ่าน promptpay ได้เต็มไม่หักจ้า

ติดตามข่าวสารแบบไว ๆ มาที่ Twitter เลย บางอย่างไม่มีในบล็อก และหน้าเพจนะ

Tags

Minseo Chayabanjonglerd

I am a full-time Android Developer and part-time contributor with developer community and web3 world, who believe people have hard skills and soft skills to up-skill to da moon.