Branch Management Tips
Branch Lifecycle Management
Listing and Inspecting Branches
## List local branches
git branch
## List all branches (local and remote)
git branch -a
## Show branches with last commit details
git branch -v
Branch Comparison
## Compare branches
git diff main feature-login
## List merged/unmerged branches
git branch --merged
git branch --no-merged
Branch Cleanup and Maintenance
Deleting Branches
## Delete a local branch
git branch -d feature-completed
## Force delete an unmerged branch
git branch -D experimental-feature
## Delete a remote branch
git push origin --delete feature-branch
Merging and Rebasing Strategies
gitGraph
commit
branch feature
commit
commit
checkout main
merge feature
Merge Techniques
## Standard merge
git checkout main
git merge feature-branch
## Squash merge (compact commit history)
git merge --squash feature-branch
Rebasing Branches
## Rebase feature branch onto main
git checkout feature-branch
git rebase main
Branch Management Best Practices
Practice |
Description |
Recommendation |
Regular Cleanup |
Remove stale branches |
Weekly/monthly |
Protection Rules |
Prevent direct commits |
Use branch protection |
Consistent Naming |
Clear branch nomenclature |
feature/fix/hotfix |
Advanced Branch Management
Stashing Uncommitted Changes
## Temporarily store changes
git stash save "Work in progress"
## List stashed changes
git stash list
## Apply most recent stash
git stash apply
Remote Branch Synchronization
## Fetch all remote branches
git fetch origin
## Prune obsolete remote tracking branches
git remote prune origin
Error Prevention Strategies
- Use branch protection rules
- Implement code review processes
- Maintain clean branch history
- Regularly synchronize branches
At LabEx, we emphasize proactive branch management to ensure smooth collaborative development.
Monitoring Branch Health
## Check branch commit status
git branch -v
## Show branches merged/not merged to current branch
git branch --merged
git branch --no-merged
Recommended Workflow
- Create feature branches from main
- Keep branches short-lived
- Merge with clear, descriptive commits
- Delete branches after successful integration