The Complete Guide to Git Branching: From Basics to Team Collaboration
Updated July 7, 2026 0 0
Introduction
Git branching is one of Git’s most powerful features and the core of team collaboration. But most tutorials only cover commands — this guide focuses on practical branching: when to create branches, which strategy to use, and how to resolve conflicts.
1. Basic Commands
Viewing Branches
# View local branches
git branch
# View all branches (including remote)
git branch -a
# View branches with last commit
git branch -v
# View branches merged into current branch
git branch --merged
# View branches not merged into current branch
git branch --no-merged
Creating Branches
# Create new branch
git branch feature-login
# Create and switch (recommended)
git switch -c feature-login
# Create based on specific branch
git switch -c feature-login origin/main
# Create orphan branch (no history)
git switch --orphan gh-pages
git rm -rf .
Switching Branches
# Switch branch (recommended)
git switch main
# Switch and restore working directory
git switch -c feature-login
# Switch to previous branch
git switch -
Merging Branches
# Switch to target branch
git switch main
# Merge feature branch
git merge feature-login
# Merge and preserve branch history
git merge --no-ff feature-login
# Cherry-pick specific commit
git cherry-pick abc123
Deleting Branches
# Delete merged branch
git branch -d feature-login
# Force delete unmerged branch
git branch -D feature-login
# Delete remote branch
git push origin --delete feature-login
# Cleanup deleted remote branch references
git fetch --prune
2. Advanced Operations
Rebase
Move current branch commits to the top of target branch:
# Interactive rebase (clean up commit history)
git rebase -i HEAD~5
# Rebase onto main
git rebase main
# Push after rebase (requires force push)
git push --force-with-lease
Rebase vs Merge:
| Scenario | Choice | Reason |
|---|---|---|
| Personal feature branch | Rebase | Keep linear history |
| Public branch | Merge | Avoid rewriting history |
| Clean up commits | Rebase -i | Combine/modify commits |
| Preserve context | Merge | Keep branch merge records |
Stashing Work
# Stash current changes
git stash
# Stash with description
git stash push -m "Working on login feature"
# Restore most recent stash
git stash pop
# Restore but keep stash
git stash apply
# View stash list
git stash list
# Restore specific stash
git stash apply stash@{2}
# Delete stash
git stash drop stash@{0}
Interactive Rebase
Clean up commit history:
git rebase -i HEAD~5
Editor shows:
pick abc1234 feat: add login button
pick def5678 fix: login button style
pick ghi9012 feat: add form validation
pick jkl3456 fix: form validation bug
pick mno7890 feat: add logout button
Common operations:
pick = Keep commit
reword = Modify commit message
edit = Modify commit content
squash = Combine with previous commit
fixup = Combine but discard commit message
drop = Delete commit
Example: Combine fix commits:
pick abc1234 feat: add login button
fixup def5678 fix: login button style
pick ghi9012 feat: add form validation
fixup jkl3456 fix: form validation bug
pick mno7890 feat: add logout button
Cherry-Pick
Select specific commits from other branches:
# Single commit
git cherry-pick abc1234
# Multiple commits
git cherry-pick abc1234 def5678
# Select without auto-commit
git cherry-pick --no-commit abc1234
3. Conflict Resolution
Conflict Types
- Content conflict: Same line modified by both sides
- Delete conflict: One side deletes, other modifies
- Rename conflict: Both sides rename the same file
Resolution Steps
# 1. View conflicted files
git status
# 2. Open and manually edit
# Conflict markers:
# <<<<<<< HEAD
# Current branch content
# =======
# Merging branch content
# >>>>>>> feature-login
# 3. Stage resolved file
git add <file>
# 4. Continue merge
git merge --continue
# Or abort merge
git merge --abort
Using Tools
# VS Code
git mergetool --tool=vscode
# JetBrains
git mergetool --tool=intellij
Preventing Conflicts
- Frequently sync main: Pull latest code daily
- Small commits: Reduce conflict scope
- Code ownership: Different people own different modules
- Use feature flags: Avoid long-lived branches
4. Branching Strategies
Git Flow
Branch Overview:
| Branch | Purpose | Lifecycle |
|---|---|---|
main | Production code | Permanent |
develop | Development integration | Permanent |
feature/* | New features | Short-lived |
release/* | Version releases | Short-lived |
hotfix/* | Emergency fixes | Short-lived |
Use Cases:
- Longer release cycles (weeks/months)
- Multiple version maintenance
- Large team collaboration
GitHub Flow
Workflow:
- Create feature branch from
main - Develop and push
- Create Pull Request
- Code review
- Merge to
main - Automatic deployment
Use Cases:
- Continuous deployment
- Small to medium teams
- Fast iteration
Trunk-Based Development
Workflow:
- All developers commit directly to
main - Use short-lived branches (< 1 day)
- Control feature release with feature flags
- Rely on automated testing for quality
Use Cases:
- Highly mature engineering teams
- Complete CI/CD pipeline
- Strong automated testing
Selection Guide
| Team Size | Release Frequency | Recommended Strategy |
|---|---|---|
| 1-5 people | Daily | GitHub Flow |
| 5-20 people | Weekly | GitHub Flow |
| 20+ people | Monthly | Git Flow |
| 100+ people | Continuous | Trunk-Based |
5. Useful Commands
Viewing History
# View branch graph
git log --oneline --graph --all
# View specific file history
git log -p <file>
# View who modified a line
git blame <file>
# View differences between branches
git diff main..feature-login
Undoing Changes
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Undo pushed commit (create new commit)
git revert abc1234
# Recover deleted branch
git reflog
git switch -d abc1234
Cleanup Operations
# Delete merged local branches
git branch --merged main | grep -v "\*\|main" | xargs -n 1 git branch -d
# Cleanup deleted remote branches
git fetch --prune
# Find large files
git rev-list --objects --all | \
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
sed -n 's/^blob //p' | sort -rnk2 | head -10
Aliases
> If you use [Oh My Zsh](/en/blog/oh-my-zsh), the Git plugin ships 100+ useful aliases out of the box.
```bash
git config --global alias.st "status"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.cm "commit -m"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.unstage "reset HEAD --"
6. Best Practices
Branch Naming Convention
feature/user-login
feature/payment-gateway
bugfix/login-error
hotfix/security-patch
release/v1.2.0
Commit Convention
feat: New feature
fix: Bug fix
docs: Documentation update
style: Code formatting
refactor: Refactoring
test: Tests
chore: Build/tools
PR Best Practices
- Small changes: Each PR does one thing
- Clear description: Explain what and why
- Link issues: Reference related issues
- Pass CI: Ensure CI passes
- Quick response: Address review comments promptly
Summary
Core principles of Git branching:
- Keep branches short-lived: Long-lived branches create conflicts
- Frequently sync main: Pull latest code daily
- Small commits: Reduce conflict scope
- Use PR/MR: Perform code reviews
- Choose the right strategy: Based on team size and release cadence
References
- Git Official Documentation - Branching — Git branching basics
- Atlassian Git Tutorials — Branching strategies explained
- GitHub Flow — GitHub’s official workflow
- Git Flow — Git Flow strategy
- Trunk-Based Development — Mainline development model
- Conventional Commits — Commit convention
- Oh My Zsh Git Plugin — Git alias list
🔗 Original Link
Share to reach more people