Effective branch management is crucial for organized development and successful pull requests. This guide covers strategies for creating, maintaining, and merging branches in open source projects.
mainormaster: Production-ready codedevelopordev: Integration branch for featuresfeature/*: New featuresbugfix/*orhotfix/*: Bug fixesrelease/*: Release preparationdocs/*: Documentation updates
# Feature branches
feature/add-user-authentication
feature/implement-payment-system
feature/ui-redesign
# Bug fixes
bugfix/fix-login-validation
bugfix/resolve-memory-leak
hotfix/critical-security-patch
# Documentation
docs/update-api-reference
docs/add-contributing-guide
# Releases
release/v2.1.0# Ensure you're on main and up to date
git checkout main
git pull origin main
# Create and switch to new branch
git checkout -b feature/new-feature-name# Create branch from current state
git checkout -b bugfix/issue-123# Create feature branch
git checkout -b feature/add-search-functionality
# Make changes and commits
git add .
git commit -m "feat: implement basic search"
# Push to remote
git push -u origin feature/add-search-functionality
# Create pull request
# After approval, merge and delete branch# Develop new features on feature branches
git checkout -b feature/new-ui-component
# Merge features to develop
git checkout develop
git merge feature/new-ui-component
# Create release branch
git checkout -b release/v1.2.0
# Merge release to main and develop
git checkout main
git merge release/v1.2.0
git checkout develop
git merge release/v1.2.0# List all branches
git branch -a
# List remote branches
git branch -r
# Show branch status
git status# Switch to existing branch
git checkout feature/login-system
# Switch and create if doesn't exist
git checkout -b feature/new-feature# Compare branches
git diff main..feature/new-feature
# See commits unique to branch
git log main..feature/new-feature --onelinegit checkout main
git merge feature/completed-feature
# Creates merge commit preserving historygit checkout main
git merge --squash feature/completed-feature
git commit -m "feat: add completed feature"
# Combines all commits into onegit checkout feature/your-branch
git rebase main
# Replays your commits on top of main# Start merge
git checkout main
git merge feature/conflicting-branch
# If conflicts occur, edit files to resolve
# Then mark as resolved
git add resolved-file.py
git commitgit checkout feature/your-branch
git rebase main
# Resolve conflicts
git add resolved-file.py
git rebase --continue
# Or abort if needed
git rebase --abort# Delete merged branch
git branch -d feature/completed-feature
# Force delete unmerged branch
git branch -D feature/abandoned-feature# Delete remote branch
git push origin --delete feature/old-feature# Remove references to deleted remote branches
git remote prune origin
# Or fetch and prune
git fetch --prune# Rebase last 3 commits
git rebase -i HEAD~3
# Commands: pick, reword, edit, squash, fixup, drop# Apply specific commit to current branch
git cherry-pick abc123
# Cherry pick multiple commits
git cherry-pick abc123 def456# Set upstream branch
git branch --set-upstream-to=origin/feature/branch
# Unset upstream
git branch --unset-upstream- Feature branches: 1-2 weeks max
- Bug fixes: Complete quickly
- Regular rebasing to stay current
# Update feature branch with main
git checkout feature/your-branch
git fetch origin
git rebase origin/main- Use conventional commits
- Reference issues:
fix: resolve issue #123 - Keep messages under 50 characters
- Never commit directly to main
- Require pull requests for merges
- Enable branch protection rules
# Set tracking
git branch --set-upstream-to=origin/your-branch# Find lost commits
git reflog
# Recover
git checkout lost-commit-hash# Reset to remote
git reset --hard origin/your-branch
# Or merge with care
git pull --no-ffEffective branch management involves:
- Consistent naming conventions
- Regular synchronization
- Appropriate merge strategies
- Clean branch lifecycle
- Conflict resolution skills
Master these practices to maintain clean, organized repositories and smooth collaboration.