1. What Is a Git Branch?
A Git Branchis simply a lightweight, movable pointer to a commit hash. Git doesn't copy your files when you create a branch; it merely creates a tiny 41-byte text file containing a commit SHA.
2. Why Do Developers Use Branches?
Work on experimental features without breaking the working production application.
10 developers can work on 10 different features simultaneously without stepping on each other's toes.
Create a Pull Request for a specific branch so teammates can review before merging into main.
3. Understanding the Main Branch
The main branch represents the canonical, production-ready timeline of your software. Developers rarely commit directly to main; all changes flow through reviewed feature branches.
4. Creating a Branch (switch vs checkout)
git switch -c feature/login-page
# Equivalent older command:
git checkout -b feature/login-page
# List all local branches (active has *):
git branch
5. Working on a Feature Branch & Switching
When you switch branches with git switch main, Git modifies your local working directory files to match the snapshot of the selected branch.
7. Making Commits on a Branch
Every commit advances only the active branch pointer (pointed to by HEAD). Other branches stay at their previous commit positions.
8. Merging Branches: Fast-Forward vs 3-Way Merge
Direct Linear History
When main hasn't moved since branching, Git simply slides main forward to the tip of the feature branch. No extra merge commit is created.
Divergent Histories
When both main and feature have new commits, Git combines both histories by generating an explicit merge commit with two parent commits.
9. Handling Merge Conflicts Step-by-Step
When both branches modify the same line, Git stops and leaves conflict markers:
const API_URL = "https://api.pathubs.com/v1";
=======
const API_URL = "https://api.pathubs.com/v2-beta";
>>>>>>> feature/api-upgrade (incoming branch)
git add, 4) Commit with git commit.10. Deleting Local & Remote Branches
git branch -d feature/navbar
# Force delete local branch (unmerged changes lost):
git branch -D feature/navbar
# Delete remote branch on GitHub:
git push origin --delete feature/navbar
11. Remote Tracking Branches (origin/...)
git push -u origin feature/auth
# Fetch all remote branches without merging:
git fetch origin
# Switch to a teammate's remote branch:
git switch feature/teammate-work
12. A Real-World Feature Branch Workflow
2. git switch -c feature/user-profile
3. Make edits, test, git add ., git commit -m "feat: profile card"
4. git push -u origin feature/user-profile
5. Open Pull Request on GitHub ➔ Code Review ➔ Merge into main
6. git switch main && git pull origin main
7. git branch -d feature/user-profile (clean up!)
13. Common Branching Mistakes
- Forgetting which branch you are on: Always run
git branchor use a terminal shell prompt with branch indicators. - Keeping long-lived stale branches: Merge frequently to avoid massive merge conflicts down the road.
- Committing on detached HEAD: Make sure you switch to a named branch before making new commits.
14. Branching Best Practices
- Use descriptive branch naming prefixes:
feat/,fix/,refactor/,docs/. - Keep branches focused on single, self-contained features.
- Delete branches immediately after they are merged into
main.