Branching Commands
Work with branches to develop features, fix bugs, and experiment safely without affecting the main codebase.
git branch
Description:
List, create, or delete branches in your repository. Branches allow you to develop features in isolation from the main codebase.
Syntax:
bash
git branch
git branch <branch-name>
git branch -d <branch-name>
git branch -D <branch-name>
git branch -m <old-name> <new-name>Examples:
bash
# List all local branches
git branch
# List all branches (local and remote)
git branch -a
# Create a new branch (but don't switch to it)
git branch feature/user-profile
# Delete a merged branch
git branch -d old-feature
# Force delete an unmerged branch
git branch -D experimental-feature
# Rename current branch
git branch -m new-branch-name
# Rename a different branch
git branch -m old-name new-name
# Show last commit on each branch
git branch -v
# Show tracking branches
git branch -vvDetailed Explanation:
- Lightweight pointers: Branches are just movable pointers to commits, not copies of files
- Doesn't switch:
git branch <name>creates a branch but keeps you on the current branch - Safe deletion:
-donly deletes if branch is fully merged;-Dforces deletion - Branch tracking: Shows which remote branch your local branch is tracking with
-vv
Common Errors:
error: branch 'name' not found→ Solution: Check spelling; usegit branchto list existing brancheserror: The branch 'name' is not fully merged→ Solution: Use-Dto force delete, or merge the branch firsterror: Cannot delete branch 'main' checked out at...→ Solution: Switch to another branch before deletingfatal: A branch named 'name' already exists→ Solution: Choose a different name or delete the existing branch first
Pro Tips:
- Use descriptive names:
feature/login,bugfix/header-alignment,hotfix/critical-security - See merged branches:
git branch --merged(safe to delete) - See unmerged branches:
git branch --no-merged(check before deleting) - Quickly list remote branches:
git branch -r - Clean up remote-tracking references:
git fetch --prune - Warning:
-Dpermanently deletes unmerged work; make sure you don't need it.
Related Commands:
git checkout– Switch to a branchgit switch– Modern way to switch branchesgit merge– Combine branchesgit push– Push branch to remote