Tools & Workflow
Git basics
Git is a version control system that tracks changes to your code over time. It lets you collaborate with others, experiment safely, and roll back mistakes.
Core Concepts
A repository (repo) is a folder tracked by Git. A commit is a saved snapshot of your changes. A branch is an independent line of development.
git init # start tracking the current foldergit clone <url> # copy an existing repogit status # see what has changedgit log --oneline # view commit historyเริ่มต้นทุก project ด้วย git init
ทำให้เป็นนิสัยที่จะรัน git init ทุกครั้งที่เริ่ม project ใหม่ แม้ว่าจะยังไม่แน่ใจว่าจะ push ขึ้น GitHub หรือเปล่า มันไม่มีผลเสียและช่วยให้ย้อนดู history ได้เสมอ
Making Commits
The basic Git workflow: edit files, stage your changes, then commit them.
git add index.html # stage a specific filegit add . # stage all changesgit commit -m "Add nav" # save a snapshot with a messageWriting Good Commit Messages
A good commit message is short, specific, and uses the imperative mood: "Add login form", not "Added login form" or "login stuff". Keep the subject under 72 characters.
# Good commit messagesgit commit -m "Add responsive navbar"git commit -m "Fix button hover color"git commit -m "Remove unused CSS variables"# Avoid vague messages like thesegit commit -m "fix stuff"git commit -m "update"git commit -m "wip"Working with Branches
Branches let you work on features without affecting the main codebase. Merge them back when ready.
git branch feature/nav # create a new branchgit switch feature/nav # switch to itgit switch -c feature/nav # create and switch in one stepgit merge feature/nav # merge into current branchgit branch -d feature/nav # delete after mergingทำงานใน branch เสมอ
อย่า commit ลง main โดยตรง สร้าง branch ใหม่สำหรับทุก feature หรือ bug fix แล้ว merge กลับเมื่อเสร็จ นิสัยนี้สำคัญมากเมื่อทำงานเป็นทีม
Remote Repositories
Push and pull changes between your local repo and a remote like GitHub.
git remote add origin <url> # link to a remote repogit push origin main # upload local commitsgit pull origin main # download remote commitsFirst push to GitHub
When pushing a new repo to GitHub for the first time, use the -u flag to set the upstream branch. After that, you can just use git push.
git push -u origin main # first push — sets upstreamgit push # all future pushesgit pull ก่อน git push เสมอ
ถ้ามีคนอื่น push ขึ้นไปก่อน คุณต้อง pull เพื่อรับการเปลี่ยนแปลงก่อน แล้วค่อย push ของคุณ ไม่งั้นจะ error