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.

Figure 1: Branching workflow

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:

ScenarioChoiceReason
Personal feature branchRebaseKeep linear history
Public branchMergeAvoid rewriting history
Clean up commitsRebase -iCombine/modify commits
Preserve contextMergeKeep 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

  1. Content conflict: Same line modified by both sides
  2. Delete conflict: One side deletes, other modifies
  3. 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

  1. Frequently sync main: Pull latest code daily
  2. Small commits: Reduce conflict scope
  3. Code ownership: Different people own different modules
  4. Use feature flags: Avoid long-lived branches

4. Branching Strategies

Git Flow

Figure 2: Git Flow branch model

Branch Overview:

BranchPurposeLifecycle
mainProduction codePermanent
developDevelopment integrationPermanent
feature/*New featuresShort-lived
release/*Version releasesShort-lived
hotfix/*Emergency fixesShort-lived

Use Cases:

  • Longer release cycles (weeks/months)
  • Multiple version maintenance
  • Large team collaboration

GitHub Flow

Figure 3: GitHub Flow

Workflow:

  1. Create feature branch from main
  2. Develop and push
  3. Create Pull Request
  4. Code review
  5. Merge to main
  6. Automatic deployment

Use Cases:

  • Continuous deployment
  • Small to medium teams
  • Fast iteration

Trunk-Based Development

Workflow:

  1. All developers commit directly to main
  2. Use short-lived branches (< 1 day)
  3. Control feature release with feature flags
  4. Rely on automated testing for quality

Use Cases:

  • Highly mature engineering teams
  • Complete CI/CD pipeline
  • Strong automated testing

Selection Guide

Team SizeRelease FrequencyRecommended Strategy
1-5 peopleDailyGitHub Flow
5-20 peopleWeeklyGitHub Flow
20+ peopleMonthlyGit Flow
100+ peopleContinuousTrunk-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

  1. Small changes: Each PR does one thing
  2. Clear description: Explain what and why
  3. Link issues: Reference related issues
  4. Pass CI: Ensure CI passes
  5. Quick response: Address review comments promptly

Summary

Core principles of Git branching:

  1. Keep branches short-lived: Long-lived branches create conflicts
  2. Frequently sync main: Pull latest code daily
  3. Small commits: Reduce conflict scope
  4. Use PR/MR: Perform code reviews
  5. Choose the right strategy: Based on team size and release cadence

References

🔗 Original Link Share to reach more people

Comments