Practical Branch Navigation
Listing and Identifying Branches
Viewing Local Branches
## List local branches
git branch
## List all branches with more details
git branch -v
## Highlight current branch
git branch --show-current
Viewing Remote Branches
## List remote branches
git branch -r
## List all branches (local and remote)
git branch -a
Branch Comparison and Management
Comparing Branches
## Compare differences between branches
git diff main..feature-branch
## Show commits not merged into main
git cherry -v main
Branch Tracking Relationships
gitGraph
commit
branch feature-branch
checkout feature-branch
commit
commit
checkout main
merge feature-branch
Branch Navigation Techniques
Technique |
Command |
Purpose |
List Branches |
git branch |
View local branches |
Create Branch |
git branch name |
Create new branch |
Delete Branch |
git branch -d name |
Remove local branch |
Rename Branch |
git branch -m old-name new-name |
Rename branch |
Advanced Branch Navigation
Tracking Remote Branches
## Create local branch tracking remote branch
git checkout -b local-branch origin/remote-branch
## Set existing local branch to track remote
git branch -u origin/remote-branch
Practical Workflow Scenarios
Scenario 1: Switching Between Feature Branches
## Quick switch to previous branch
git checkout -
## List recent branches
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | head -n 5
Scenario 2: Cleaning Up Branches
## Delete merged branches
git branch --merged | egrep -v "(^\*|main|master)" | xargs git branch -d
LabEx Recommended Practices
- Maintain clean and organized branch structure
- Use meaningful branch names
- Regularly prune unnecessary branches
- Utilize branch tracking for efficient collaboration
Common Branch Navigation Challenges
- Managing complex branch hierarchies
- Tracking remote branch changes
- Resolving merge conflicts
- Maintaining branch hygiene