🏠 Home / Hub

🔶 Git Lesson 02 — Branches

← Back to Git Menu  |  🏠 Hub

1. Branch ဆိုတာ

Branch = separate development line — main code ကို မထိဘဲ feature develop

main/master = production code (stable)
feature/login = new feature develop
fix/navbar-bug = bug fix

Branch = lightweight pointer to a commit — disk space မများ!
# Visualize branches:
main     ── A ── B ── C ──────────────── M (merge)
feature           └── D ── E ── F ──────┘

2. Branch Commands

# List branches
git branch           # local branches
git branch -a        # all (including remote)
git branch -v        # with last commit

# Create branch
git branch feature/login

# Switch to branch
git switch feature/login    # modern (Git 2.23+)
git checkout feature/login  # old way (still works)

# Create AND switch (shortcut)
git switch -c feature/login    # new way
git checkout -b feature/login  # old way

# Delete branch
git branch -d feature/login   # safe delete (merged only)
git branch -D feature/login   # force delete

# Rename branch
git branch -m old-name new-name

# See current branch
git branch --show-current

3. Branch Workflow

# Standard workflow

# 1. Start from main (always pull first!)
git switch main
git pull origin main

# 2. Create feature branch
git switch -c feature/user-profile

# 3. Work on feature
# ... edit files ...
git add .
git commit -m "add user profile page"
git commit -m "add profile avatar upload"
git commit -m "add profile form validation"

# 4. Push branch to GitHub (share with team)
git push origin feature/user-profile

# 5. Create Pull Request (GitHub → New PR)

# 6. After merge, clean up
git switch main
git pull origin main
git branch -d feature/user-profile

# Useful: see branch graph
git log --all --oneline --graph

4. Branch Naming Convention

TypePatternExample
Featurefeature/descriptionfeature/user-login
Bug Fixfix/descriptionfix/nav-overflow
Hotfixhotfix/descriptionhotfix/security-patch
Releaserelease/versionrelease/v2.1.0
Chorechore/descriptionchore/update-deps
Docsdocs/descriptiondocs/api-readme
💡 Lowercase, hyphen-separated, descriptive — avoid: fix123, myBranch, test

5. HEAD — Where You Are

# HEAD = current position pointer

# Normal: HEAD points to branch, branch points to latest commit
main ← HEAD
 └── commit A ← commit B ← commit C

# "Detached HEAD" — HEAD points directly to commit (not branch)
git checkout abc1234   # go to specific commit
# ⚠️ Detached HEAD — commits here won't be saved to branch!
# To save work: git switch -c new-branch-name

# HEAD~n — relative references
HEAD       = current commit
HEAD~1     = one commit back
HEAD~2     = two commits back
HEAD^      = same as HEAD~1 (parent)

# Example
git log --oneline
# c3f4d5e add contact page
# b2a1f3a add about page
# a5e6c7d initial commit

git diff HEAD~1   # compare current with previous commit
git show HEAD~2   # view the commit 2 back

← Git 01  |  Next: Git 03 → Merge & Conflicts →

📌 Study Checklist