# Visualize branches: main ── A ── B ── C ──────────────── M (merge) feature └── D ── E ── F ──────┘
# 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
# 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
| Type | Pattern | Example |
|---|---|---|
| Feature | feature/description | feature/user-login |
| Bug Fix | fix/description | fix/nav-overflow |
| Hotfix | hotfix/description | hotfix/security-patch |
| Release | release/version | release/v2.1.0 |
| Chore | chore/description | chore/update-deps |
| Docs | docs/description | docs/api-readme |
# 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 →