Git Branching
A branch is an independent line of development within a Git repository. It allows working on new features, bug fixes, or experiments in isolation without affecting the main codebase.
The default branch in a repository is typically called main (or master in older projects). Every other branch is created from this base.
Real-life analogy: Think of a tree. The trunk is the main, stable code. When starting work on a new feature, a new branch grows from the trunk. When the feature is complete and tested, the branch is merged back into the trunk.
main: A ──── B ──── C ────────────────── G
\ /
feature: D ──── E ──── F ──
Commits D, E, F are on the feature branch and do not affect main until they are merged.
Why Use Branches?
- Keep the main branch clean and always deployable
- Multiple developers can work on different features simultaneously
- Experiment with changes without breaking working code
- Fix bugs in production without interrupting feature development
- Each feature or fix gets its own isolated workspace
Branch Commands Overview
List All Local Branches
git branchOutput:
* main
feature-login
fix-header-bug
The * symbol shows the currently active branch.
List All Branches (Local + Remote)
git branch -aList Remote Branches Only
git branch -rCreating a Branch
git branch feature-loginCreates a new branch called feature-login but does NOT switch to it. The active branch remains the same.
Creating and Switching in One Step (Recommended)
# Modern syntax (Git 2.23+)
git switch -c feature-login
# Older syntax (still works)
git checkout -b feature-login
Both commands create a new branch and immediately switch to it.
Switching Between Branches
# Modern syntax
git switch main
# Older syntax
git checkout main
Full Branch Workflow Example
# Step 1: Confirm current position
git branch
# Output: * main
# Step 2: Create and switch to a new feature branch
git switch -c feature-contact-form
# Step 3: Work on the feature
echo "<form>...</form>" > contact.html
git add contact.html
git commit -m "Add contact form"
# Step 4: Continue developing
echo "form { margin: 20px; }" >> style.css
git add style.css
git commit -m "Style the contact form"
# Step 5: Switch back to main
git switch main
# Step 6: See that contact.html is NOT in main (it only exists on the branch)
ls
# index.html about.html (no contact.html here)
Renaming a Branch
# Rename the currently active branch
git branch -m new-name
# Rename a specific branch (while on a different branch)
git branch -m old-name new-name
Deleting a Branch
Delete a Merged Branch (Safe)
git branch -d feature-loginThe -d flag only works if the branch has already been merged. This prevents accidental loss of unmerged work.
Force Delete an Unmerged Branch
git branch -D feature-loginThe uppercase -D forces deletion even if the branch has not been merged. Use with caution — the unmerged work will be lost.
Pushing a Branch to GitHub
git push origin feature-loginUploads the local branch to GitHub so others can see and collaborate on it.
Deleting a Remote Branch
git push origin --delete feature-loginViewing Branch History
git log --oneline --graph --allShows a visual diagram of all commits and branches.
Branch Naming Conventions
Good branch names describe what the branch is for. Common conventions:
| Convention | Examples |
|---|---|
| Feature prefix | feature/user-login, feature/payment-gateway |
| Bug fix prefix | fix/null-pointer-error, bugfix/login-crash |
| Hotfix prefix | hotfix/security-patch |
| Release prefix | release/v2.0, release/2024-03 |
Summary
Branches let different work happen in parallel without interfering with the main codebase. The git switch -c branch-name command creates and switches to a new branch. After finishing the work on a branch, it is merged back into main. Always delete branches after merging to keep the repository clean.
