Git 104: branch ช่วยให้ทดลองอย่างปลอดภัยGit 104: branches let you experiment safely
ใช้ branch เพื่อแยกงาน สลับเส้นทางทำงาน merge change เล็กๆ และเลี่ยงการปนงานที่ไม่เกี่ยวกันUse branches to isolate work, switch between lines of work, merge a small change, and avoid mixing unrelated edits.
A branch is a separate line of work.
Use a branch when a change should be isolated until you inspect it.
See your current branch
Run:
git branch
The current branch has a marker next to it.
Many projects use main as the default branch.
Create and switch to a branch
Create a branch for a small README improvement:
git switch -c docs-add-learning-goal
Read this as:
Create a branch and switch to it.
Now edit README.md:
## Learning goal
Practice checking status and diff before saving work.
Commit the change:
git status
git diff
git add README.md
git commit -m "docs: add learning goal"
Switch back to main
Run:
git switch main
Open the file again. The learning goal may disappear because that change exists on your branch, not on main yet.
That is the point. Branches keep work isolated.
Merge the branch
When the branch is ready:
git merge docs-add-learning-goal
Now the change becomes part of the current branch.
Check:
git log --oneline --graph --all -5
git status
Delete a finished local branch
After the branch is merged:
git branch -d docs-add-learning-goal
This deletes the branch name, not the commit history that was merged.
Branch naming habits
Use names that make the purpose clear:
docs-add-learning-goal
fix-login-redirect
experiment-new-layout
Avoid:
stuff
try
new
Your future self and reviewers need context.
When not to switch
Do not switch branches with messy uncommitted work unless you know what will happen.
Before switching:
git status
If you have uncommitted changes, decide:
- commit them
- discard them if they are truly not needed
- stash them if you know how stash works
For beginners, a clean working tree is the safest branch-switching state.
Practice: branch checklist
Fill this before starting a branch:
Branch name:
Goal:
Files I expect to change:
How I will verify:
What should stay untouched:
That last line matters when working with AI agents. It tells the agent what not to edit.
branch คือเส้นทางทำงานแยก
ใช้ branch เมื่อ change ควรถูกแยกไว้ก่อนจนกว่าจะตรวจเรียบร้อย
ดู branch ปัจจุบัน
รัน:
git branch
branch ปัจจุบันจะมีเครื่องหมายอยู่ข้างหน้า
หลาย project ใช้ main เป็น default branch
สร้างและสลับไป branch ใหม่
สร้าง branch สำหรับปรับ README เล็กๆ:
git switch -c docs-add-learning-goal
อ่านได้ว่า:
สร้าง branch และสลับไป branch นั้น
จากนั้นแก้ README.md:
## Learning goal
Practice checking status and diff before saving work.
commit change:
git status
git diff
git add README.md
git commit -m "docs: add learning goal"
สลับกลับ main
รัน:
git switch main
เปิดไฟล์อีกครั้ง learning goal อาจหายไป เพราะ change นั้นอยู่บน branch ของคุณ ยังไม่ได้อยู่บน main
นี่คือประโยชน์ของ branch มันแยกงานออกจากกัน
Merge branch
เมื่อ branch พร้อม:
git merge docs-add-learning-goal
ตอนนี้ change จะเข้ามาใน branch ปัจจุบัน
เช็ก:
git log --oneline --graph --all -5
git status
ลบ branch local ที่เสร็จแล้ว
หลัง merge แล้ว:
git branch -d docs-add-learning-goal
สิ่งนี้ลบชื่อ branch ไม่ได้ลบ commit history ที่ merge เข้าไปแล้ว
นิสัยการตั้งชื่อ branch
ใช้ชื่อที่บอกเป้าหมาย:
docs-add-learning-goal
fix-login-redirect
experiment-new-layout
หลีกเลี่ยง:
stuff
try
new
ตัวคุณในอนาคตและ reviewer ต้องการ context
เมื่อไหร่ไม่ควร switch
อย่า switch branch พร้อม uncommitted work ที่ยุ่ง ถ้ายังไม่รู้ว่าจะเกิดอะไร
ก่อน switch:
git status
ถ้ามี uncommitted changes ให้ตัดสินใจ:
- commit
- discard ถ้ามั่นใจว่าไม่ต้องใช้จริง
- stash ถ้าคุณเข้าใจ stash แล้ว
สำหรับมือใหม่ working tree ที่ clean คือสภาพที่ปลอดภัยที่สุดก่อนสลับ branch
แบบฝึก: checklist ก่อนสร้าง branch
เติมสิ่งนี้ก่อนเริ่ม branch:
ชื่อ branch:
เป้าหมาย:
ไฟล์ที่คาดว่าจะแก้:
จะ verify อย่างไร:
อะไรที่ไม่ควรถูกแตะ:
บรรทัดสุดท้ายสำคัญมากเมื่อต้องทำงานกับ AI agent เพราะมันบอก agent ว่าอะไรห้ามแก้
เข้าสู่ระบบเพื่อบันทึกความคืบหน้าSign in to track your progress
เข้าสู่ระบบSign in→