ในบทความนี้
In this post
ก่อนหน้านี้เราได้พูดถึงพื้นฐานของ Git ว่ามันคืออะไร ทำไมต้องใช้ แล้วมี commit กับ repository ยังไงบ้าง พอเราใช้ Git เป็นแล้ว คำถามต่อมาคือ ถ้ามีหลายคนทำงานบนโปรเจกต์เดียวกัน หรือเราอยากลองทำฟีเจอร์ใหม่โดยไม่พัง code หลัก จะทำยังไงดี?
คำตอบคือ Branching — หรือการ "แตกกิ่ง" ออกจากสายหลักของ code เพื่อทำงานแยก แล้วค่อย merge กลับมาทีหลัง
Branch คืออะไร?
Branch ใน Git ก็คือ ตัวชี้ (pointer) ไปยัง commit ตัวหนึ่ง เวลาเราสร้าง branch ใหม่ Git ไม่ได้ copy ไฟล์ทั้งหมด แค่สร้าง pointer ตัวใหม่ขึ้นมาชี้ไปที่ commit เดิม ทำให้การสร้าง branch เร็วมากและแทบไม่เปลืองพื้นที่เลย
จากตัวอย่าง:
mainเป็น branch หลัก มี commit A, B, Cfeatureแตกออกจาก C แล้วมี commit D, E ต่อไปเอง- ทั้งสอง branch ไม่กระทบกัน จนกว่าจะ merge
ทำไมต้อง Branch?
1. ทำงานพร้อมกันได้
สมมติทีมมี 3 คน:
- คนที่ 1 ทำ Login page
- คนที่ 2 ทำ Dashboard
- คนที่ 3 แก้ Bug ด่วน
ถ้าทุกคนทำบน branch เดียวกัน จะวุ่นวายมาก code ชนกัน commit ทับกัน ทำให้เสียเวลาแก้ conflict แต่ถ้าแต่ละคน แตก branch ของตัวเอง ก็ทำงานได้อิสระ เสร็จแล้วค่อย merge กลับ
2. ลองของใหม่ได้โดยไม่เสี่ยง
อยากลอง refactor ครั้งใหญ่? แตก branch ทดลองเลย ถ้าพัง ก็แค่ลบ branch ทิ้ง main ไม่โดนผลกระทบเลย
3. Review code ก่อน merge
ในทีมที่ทำงานจริง เราจะไม่ merge เข้า main ตรงๆ แต่จะเปิด Pull Request (PR) เพื่อให้คนอื่นมา review code ก่อน ซึ่งต้องใช้ branch ในการทำ
คำสั่งพื้นฐาน
Git branch commands ง่ายมาก — สร้าง, สลับ, ดู, ลบ แค่ 4 อย่าง เพราะ branch ใน Git เป็นแค่ pointer (ไม่ copy ไฟล์) ทุกอย่างจึงเร็วมาก ไม่ว่าโปรเจกต์จะใหญ่แค่ไหน:
สร้าง Branch ใหม่
git branch feature-login
คำสั่งนี้สร้าง branch ชื่อ feature-login แต่เรายังอยู่บน branch เดิมอยู่
สลับไป Branch อื่น
สลับ branch ทำให้ working directory เปลี่ยนเป็นไฟล์ของ branch นั้นทันที — เหมือนเปิดโฟลเดอร์คนละชุด แต่จริงๆ Git สลับไฟล์ให้เร็วมาก:
git checkout feature-login
# หรือแบบสั้นกว่า (สร้าง + สลับในคำสั่งเดียว)
git checkout -b feature-login
ใน Git เวอร์ชันใหม่ (2.23+) สามารถใช้ git switch แทนได้:
git switch feature-login
git switch -c feature-login # สร้าง + สลับ
ดู Branch ทั้งหมด
ดูว่าตอนนี้มี branch อะไรบ้าง เครื่องหมาย * บอกว่ากำลังอยู่ branch ไหน เพิ่ม -a เพื่อดู remote branches ด้วย:
git branch # ดูเฉพาะ local
git branch -a # ดูทั้ง local + remote
ลบ Branch
Branch ที่ merge แล้วควรลบทิ้ง — ไม่งั้น branch list จะยาวเป็นหางม้า ใช้ -d (safe delete — ต้อง merge แล้ว) หรือ -D (force delete — ยังไม่ merge ก็ลบ):
git branch -d feature-login # ลบ (ถ้า merge แล้ว)
git branch -D feature-login # ลบบังคับ (ยังไม่ merge ก็ลบ)
Merge — รวม Branch กลับ
เมื่อทำงานบน feature branch เสร็จแล้ว ต้องรวม code กลับเข้า main — นี่คือ merge Git มี 2 วิธี merge: Fast-Forward (เลื่อน pointer ไปข้างหน้า — ง่ายสุด) และ Three-Way Merge (สร้าง merge commit ใหม่ — เมื่อทั้งสอง branch มี commits ต่างกัน):
git checkout main
git merge feature-login
Fast-Forward Merge
ถ้า main ไม่มี commit ใหม่ตั้งแต่แตก branch Git จะแค่ เลื่อน pointer ไปข้างหน้า เรียกว่า fast-forward:
ก่อน merge:
หลัง merge (fast-forward):
Three-Way Merge
ถ้า main มี commit ใหม่เพิ่มมาด้วย Git จะสร้าง merge commit ขึ้นมาเชื่อมสอง branch:
ก่อน merge:
หลัง merge (three-way):
โดย M คือ merge commit ที่รวมทั้งสอง branch เข้าด้วยกัน
Merge Conflict — เมื่อ code ชนกัน
Conflict เป็นเรื่องปกติ ไม่ต้องกลัว! เกิดเมื่อทั้งสอง branch แก้ไข ไฟล์เดียวกัน บรรทัดเดียวกัน — Git ไม่รู้ว่าจะเอาเวอร์ชันไหน จึงบอกให้เรา "เลือกเอง" วิธีหลีกเลี่ยง: merge บ่อยๆ อย่าปล่อยให้ branch ห่างจาก main นานเกินไป แต่ถ้าเจอก็แก้ได้ง่าย:
<<<<<<< HEAD
console.log("version from main");
=======
console.log("version from feature");
>>>>>>> feature-login
วิธีแก้:
- เปิดไฟล์ที่ conflict
- เลือกว่าจะเอา code ส่วนไหน (หรือรวมทั้งสอง)
- ลบ
<<<<<<<,=======,>>>>>>>ออก git addแล้วgit commit
Branching Strategy ยอดนิยม
Branching strategy คือ "กฎ" ของทีมว่าจะใช้ branch ยังไง — ไม่มีสูตรตายตัว เลือกตามขนาดทีม, release frequency, และ risk tolerance ส่วนใหญ่ใช้ 1 ใน 3 แบบนี้:
1. Git Flow
เป็นรูปแบบที่เป็นระบบมากที่สุด มี branch หลายประเภท (main, develop, feature, release, hotfix) เหมาะกับโปรเจกต์ที่มี release cycle ชัดเจน เช่น mobile apps ที่ต้อง submit app store — แต่ซับซ้อนเกินไปสำหรับทีมเล็กหรือ web apps ที่ deploy ทุกวัน:
| Branch | หน้าที่ |
|---|---|
main | code ที่ deploy อยู่บน production |
develop | branch รวม feature ที่พัฒนาเสร็จแล้ว |
feature/* | branch สำหรับแต่ละ feature |
release/* | เตรียม release (ทดสอบ + แก้ bug) |
hotfix/* | แก้ bug ด่วนบน production |
2. GitHub Flow
เรียบง่ายกว่า Git Flow มาก — มีแค่ main + feature branches ทำงานเสร็จ → เปิด PR → review → merge → deploy อัตโนมัติ เหมาะกับทีมที่ deploy บ่อยๆ ผ่าน CI/CD — เป็น strategy ที่ทีมส่วนใหญ่ควรเริ่มต้นใช้:
- แตก branch จาก
main - ทำงาน + commit
- เปิด Pull Request
- Review + approve
- Merge เข้า
main - Deploy
3. Trunk-Based Development
เรียบง่ายสุดๆ ทุกคน commit เข้า main ตรงๆ แต่ใช้ feature flags เปิด/ปิดฟีเจอร์แทน เหมาะกับทีมที่มีประสบการณ์สูง + มี CI/CD ที่แข็งแกร่ง
เปรียบเทียบ Branching Strategy
สรุปข้อดีข้อเสียของแต่ละ strategy — เลือกตามทีม ไม่มีถูกผิด ทีมเล็ก deploy บ่อยเลือก GitHub Flow ทีมใหญ่มี release cycle เลือก Git Flow ทีมเก่งมี CI/CD แข็งเลือก Trunk-Based:
| Git Flow | GitHub Flow | Trunk-Based | |
|---|---|---|---|
| ความซับซ้อน | สูง | ต่ำ | ต่ำมาก |
| เหมาะกับ | Release cycle ยาว | Deploy บ่อย | Deploy ทุก commit |
| จำนวน branch | เยอะ (5+ ประเภท) | น้อย (main + feature) | แทบไม่มี |
| ข้อเสีย | ซับซ้อนเกินไปสำหรับทีมเล็ก | ไม่เหมาะกับ release ที่ต้องวางแผน | ต้องมี CI/CD ที่ดีมาก |
คำสั่งที่ใช้บ่อย — สรุป
รวม Git branch commands ที่ใช้บ่อยที่สุด — จำไม่ได้ก็ไม่เป็นไร แค่รู้ว่ามีอะไรบ้าง พิมพ์ git branch --help เมื่อต้องการ หรือใช้ GUI tools อย่าง VS Code, GitKraken, SourceTree ก็สะดวกดี:
# สร้าง + สลับ branch
git switch -c feature-name
# push branch ไป remote
git push -u origin feature-name
# ดึง branch จาก remote
git fetch origin
git switch feature-name
# merge กลับ main
git switch main
git merge feature-name
# ลบ branch (local + remote)
git branch -d feature-name
git push origin --delete feature-name
# ดู branch graph สวยๆ
git log --oneline --graph --all
สรุป
Git Branching เป็นทักษะพื้นฐานที่ developer ทุกคนต้องเชี่ยวชาญ — ไม่ว่าจะทำงานคนเดียวหรือทีม 100 คน branch ทำให้ทำงานพร้อมกันได้โดยไม่ชนกัน, ลองของใหม่ได้โดยไม่เสี่ยง, และ review code ก่อน merge ได้ เลือก branching strategy ที่เหมาะกับทีม แล้ว CI/CD pipeline จะทำที่เหลือให้:
- Branch = pointer ไปยัง commit → สร้างเร็ว ไม่เปลืองพื้นที่
- ใช้ branch เพื่อ ทำงานพร้อมกัน ทดลองของใหม่ และ review code
- Merge คือการรวม branch กลับ → อาจเจอ conflict ถ้าแก้ไฟล์เดียวกัน
- เลือก branching strategy ตามขนาดทีมและ deployment cycle
- merge บ่อยๆ = conflict น้อย → ชีวิตดี
ในโพสต์หน้าเราจะมาพูดถึง Rebase — อีกวิธีในการรวม branch ที่ทำให้ประวัติ commit สะอาดขึ้น แต่มีข้อควรระวังที่สำคัญมาก! 🐕
Earlier we talked about the fundamentals of Git — what it is, why we use it, and how commits and repositories fit together. Once you can use Git, the next question arrives: if several people are working on the same project, or you want to try a new feature without breaking the main code, what do you do?
The answer is branching — growing a branch off the main line of code so you can work separately, then merging it back later.
What Is a Branch?
A branch in Git is simply a pointer to one commit. When we create a new branch, Git does not copy any files — it just creates a new pointer aimed at the existing commit, which makes creating a branch extremely fast and almost free in disk space.
From the example:
mainis the primary branch, holding commits A, B and Cfeaturebranched off C and then grew commits D and E of its own- the two branches do not affect each other until they are merged
Why Branch At All?
1. Everyone can work at once
Suppose a team of three:
- the first person is building the login page
- the second is building the dashboard
- the third is fixing an urgent bug
If all three work on the same branch it turns into chaos — code collides, commits land on top of each other, and time disappears into resolving conflicts. But if each person grows their own branch, they work independently and merge back when they are done.
2. Try new things without risk
Want to attempt a big refactor? Grow a branch and experiment. If it falls apart, just delete the branch — main is untouched.
3. Review code before merging
On a real team we never merge straight into main. We open a Pull Request (PR) so someone else can review the code first, and that requires a branch.
The Basic Commands
Git's branch commands are simple — create, switch, list, delete, and that is all four of them. Because a branch in Git is only a pointer (no files are copied), everything is fast no matter how large the project is:
Create a New Branch
git branch feature-login
This creates a branch named feature-login, but we are still standing on the branch we started from.
Switch to Another Branch
Switching branches changes the working directory to that branch's files immediately — as if you had opened a different folder, except that Git swaps the files for you very quickly:
git checkout feature-login
# or the shorter form (create + switch in one command)
git checkout -b feature-login
In newer versions of Git (2.23+) you can use git switch instead:
git switch feature-login
git switch -c feature-login # create + switch
List Every Branch
See which branches exist right now; the * marks the one you are on. Add -a to see the remote branches too:
git branch # local only
git branch -a # local + remote
Delete a Branch
A branch that has been merged should be deleted — otherwise the branch list ends up as long as a horse's tail. Use -d (safe delete — must already be merged) or -D (force delete — removes it even unmerged):
git branch -d feature-login # delete (if already merged)
git branch -D feature-login # force delete (even if not merged)
Merge — Bringing a Branch Back
When the work on a feature branch is finished, the code has to come back into main — that is a merge. Git has two ways of doing it: fast-forward (move the pointer forward — the simplest case) and a three-way merge (create a new merge commit — for when both branches have commits of their own):
git checkout main
git merge feature-login
Fast-Forward Merge
If main has gained no new commits since the branch was created, Git simply moves the pointer forward. That is called a fast-forward:
Before the merge:
After the merge (fast-forward):
Three-Way Merge
If main has picked up new commits as well, Git creates a merge commit to join the two branches:
Before the merge:
After the merge (three-way):
Here M is the merge commit that brings the two branches together.
Merge Conflict — When Code Collides
Conflicts are normal — do not be afraid of them! One happens when both branches changed the same line of the same file: Git does not know which version to keep, so it asks us to choose. How to avoid them: merge often, and never let a branch drift away from main for too long. And when one does turn up, it is easy to fix:
<<<<<<< HEAD
console.log("version from main");
=======
console.log("version from feature");
>>>>>>> feature-login
How to fix it:
- open the conflicted file
- decide which side of the code to keep (or combine both)
- remove the
<<<<<<<,=======and>>>>>>>markers git add, thengit commit
Popular Branching Strategies
A branching strategy is the team's "rules" for how branches get used — there is no single right formula. Choose by team size, release frequency and risk tolerance. Most teams use one of these three:
1. Git Flow
The most structured of the three, with several kinds of branch (main, develop, feature, release, hotfix). It suits projects with a clear release cycle — mobile apps that have to be submitted to an app store, for instance — but it is too complex for a small team or a web app that deploys every day:
| Branch | Role |
|---|---|
main | the code currently deployed to production |
develop | the branch that collects finished features |
feature/* | a branch per feature |
release/* | preparing a release (testing + bug fixes) |
hotfix/* | urgent fixes to production |
2. GitHub Flow
Far simpler than Git Flow — just main plus feature branches. Finish the work → open a PR → review → merge → deploy automatically. It suits teams that deploy often through CI/CD, and it is the strategy most teams should start with:
- branch off
main - work and commit
- open a Pull Request
- review and approve
- merge into
main - deploy
3. Trunk-Based Development
Simplest of all: everyone commits straight into main, but uses feature flags to turn features on and off instead. It suits highly experienced teams with strong CI/CD behind them.
Comparing the Branching Strategies
The strengths and weaknesses of each strategy side by side — pick the one that fits the team; none of them is wrong. A small team that deploys often picks GitHub Flow, a large team with a release cycle picks Git Flow, a strong team with solid CI/CD picks trunk-based:
| Git Flow | GitHub Flow | Trunk-Based | |
|---|---|---|---|
| Complexity | High | Low | Very low |
| Best for | Long release cycles | Frequent deploys | Deploying every commit |
| Number of branches | Many (5+ kinds) | Few (main + feature) | Almost none |
| Drawback | Too complex for a small team | Poor fit for releases that need planning | Demands very good CI/CD |
The Commands You Use Most — A Summary
The Git branch commands that come up most often — and it is fine not to memorise them, as long as you know what exists. Type git branch --help when you need it, or reach for a GUI tool such as VS Code, GitKraken or SourceTree:
# create + switch branch
git switch -c feature-name
# push the branch to the remote
git push -u origin feature-name
# fetch a branch from the remote
git fetch origin
git switch feature-name
# merge back into main
git switch main
git merge feature-name
# delete the branch (local + remote)
git branch -d feature-name
git push origin --delete feature-name
# see a nice branch graph
git log --oneline --graph --all
Summary
Git branching is a fundamental skill every developer has to master — whether you work alone or on a team of a hundred. Branches let people work at the same time without colliding, let you try new things without risk, and let code be reviewed before it is merged. Choose the branching strategy that fits your team, and the CI/CD pipeline will take care of the rest:
- a branch = a pointer to a commit → fast to create, cheap in space
- use branches to work in parallel, experiment, and review code
- merging brings a branch back → you may hit a conflict if the same file was changed
- choose a branching strategy to match your team size and deployment cycle
- merge often = fewer conflicts → a better life
In the next post we turn to rebase — another way of combining branches that leaves a cleaner commit history, but comes with some very important caveats! 🐕