Git Branch Management on macOS
Introduction
Git branches are one of Git’s most powerful features. This post introduces the basic concepts and common commands for Git branch management.
Viewing Branches
# View local branches
git branch
# View all branches (including remote)
git branch -a
# View branches with last commit info
git branch -v
Creating Branches
# Create a new branch
git branch feature-login
# Create and switch to a new branch
git checkout -b feature-login
# Create a new branch (recommended)
git switch -c feature-login
Switching Branches
# Switch to a specific branch
git checkout main
# Switch branch (recommended)
git switch main
Merging Branches
# Switch to the target branch
git checkout main
# Merge the feature branch
git merge feature-login
Deleting Branches
# Delete merged branch
git branch -d feature-login
# Force delete unmerged branch
git branch -D feature-login
Resolving Conflicts
When merge conflicts occur, Git marks the conflicting files. You need to resolve them manually:
- Open the conflicting file
- Find conflict markers (
<<<<<<<,=======,>>>>>>>) - Manually edit to keep the correct content
- Add to staging and commit
git add .
git commit -m "feat: resolve merge conflict"
Branch Management Strategy
Recommended to use Git Flow or GitHub Flow workflow:
main- Production branchdevelop- Development branchfeature/*- Feature brancheshotfix/*- Hotfix branches
Summary
Mastering Git branch management is fundamental for team collaboration. Practice creating, switching, and merging branches regularly.