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 folder
git clone <url> # copy an existing repo
git status # see what has changed
git 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 file
git add . # stage all changes
git commit -m "Add nav" # save a snapshot with a message

Writing 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 messages
git commit -m "Add responsive navbar"
git commit -m "Fix button hover color"
git commit -m "Remove unused CSS variables"
# Avoid vague messages like these
git 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 branch
git switch feature/nav # switch to it
git switch -c feature/nav # create and switch in one step
git merge feature/nav # merge into current branch
git 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 repo
git push origin main # upload local commits
git pull origin main # download remote commits

First 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 upstream
git push # all future pushes

git pull ก่อน git push เสมอ

ถ้ามีคนอื่น push ขึ้นไปก่อน คุณต้อง pull เพื่อรับการเปลี่ยนแปลงก่อน แล้วค่อย push ของคุณ ไม่งั้นจะ error

Previous
Next.js basics