Git Branching and Merging
Git branching is one of the most powerful features of Git. This tutorial covers how to create, manage, and merge branches in your Git repository.
What are Branches?
In Git, a branch is essentially a movable pointer to a specific commit. Branches allow you to work on new features or fixes without affecting the main codebase.
Basic Branch Operations
1. Creating Branches
# Create a new branch (but stay on current branch)
git branch feature-login
# Create and switch to a new branch
git checkout -b feature-login
# Modern Git (2.23+) - create and switch
git switch -c feature-login2. Switching Branches
# Switch to an existing branch
git checkout feature-login
# Modern Git (2.23+)
git switch feature-login
# Switch back to main branch
git checkout main
# or
git switch main3. Listing Branches
# List all local branches
git branch
# List all branches (local and remote)
git branch -a
# List branches with latest commit info
git branch -v
# Show merged branches
git branch --merged
# Show unmerged branches
git branch --no-merged4. Deleting Branches
# Delete a branch (must be merged)
git branch feature-login
# Force delete a branch (even if not merged)
git branch -D feature-login
# Delete remote branch
git push origin --delete feature-loginBranch Workflow Example
Let’s walk through a typical feature development workflow:
# 1. Start on main branch
git checkout main
git pull origin main
# 2. Create a new feature branch
git checkout -b feature-user-profile
# 3. Make changes
echo "User profile feature" > profile.txt
git add profile.txt
git commit -m "Add user profile feature"
# 4. Make more changes
echo "Profile settings" > settings.txt
git add settings.txt
git commit -m "Add profile settings"
# 5. Switch back to main
git checkout main
# 6. Merge the feature branch
git merge feature-user-profile
# 7. Push changes
git push origin main
# 8. Delete the feature branch
git branch -d feature-user-profileUnderstanding Merge Types
1. Fast-Forward Merge
Occurs when the branch being merged is ahead of the current branch.
# Main branch: A---B
# Feature branch: A---B---C---D
# When merging feature into main:
git checkout main
git merge feature
# Result: A---B---C---D (fast-forward)2. Recursive Merge (3-way merge)
Occurs when branches have diverged.
# Main branch: A---B---E
# Feature branch: A---B---C---D
# When merging feature into main:
git checkout main
git merge feature
# Result: A---B---E---F (where F is a merge commit)Handling Merge Conflicts
When Git can’t automatically merge changes, you’ll need to resolve conflicts manually.
Example Conflict Scenario
# On main branch
echo "Main version" > file.txt
git add file.txt
git commit -m "Update file on main"
# Switch to feature branch
git checkout feature
echo "Feature version" > file.txt
git add file.txt
git commit -m "Update file on feature"
# Try to merge
git checkout main
git merge feature
# CONFLICT!Resolving Conflicts
Git will mark conflicts in your files:
<<<<<<< HEAD
Main version
=======
Feature version
>>>>>>> featureTo resolve:
- Open the conflicted file
- Choose which version to keep (or combine both)
- Remove the conflict markers
- Add and commit the resolved file
# After resolving conflicts
git add file.txt
git commit -m "Resolve merge conflict"Advanced Branching Strategies
1. Feature Branch Workflow
Each new feature gets its own branch.
# Create feature branches
git checkout -b feature-authentication
git checkout -b feature-payment-gateway
git checkout -b feature-user-dashboard
# Work on features independently
# Merge when complete
git checkout main
git merge feature-authentication
git merge feature-payment-gateway
git merge feature-user-dashboard2. Git Flow Workflow
More structured approach with main, develop, feature, release, and hotfix branches.
# Create develop branch
git checkout -b develop main
# Create feature branches from develop
git checkout -b feature-new-button develop
# When feature is complete, merge to develop
git checkout develop
git merge feature-new-button
# Create release branch from develop
git checkout -b release-v1.0 develop
# After testing, merge to main and develop
git checkout main
git merge release-v1.0
git checkout develop
git merge release-v1.03. Release Branch Workflow
For preparing production releases.
# Create release branch
git checkout -b release-1.2.0 develop
# Fix bugs, update version numbers
git commit -m "Fix critical bug for release"
# Merge to main for production
git checkout main
git merge release-1.2.0
git tag v1.2.0
# Merge back to develop
git checkout develop
git merge release-1.2.04. Hotfix Workflow
For fixing production issues quickly.
# Create hotfix from main
git checkout main
git checkout -b hotfix-critical-bug
# Fix the issue
git commit -m "Fix critical security bug"
# Merge to main and tag
git checkout main
git merge hotfix-critical-bug
git tag v1.2.1
# Merge to develop
git checkout develop
git merge hotfix-critical-bugRemote Branches
1. Pushing Branches
# Push current branch to remote
git push origin feature-login
# Push all branches
git push --all origin
# Set up tracking for new branch
git push -u origin feature-login2. Tracking Remote Branches
# List remote branches
git branch -r
# Create local branch tracking remote
git checkout -b feature-login origin/feature-login
# Modern Git
git switch feature-login # Automatically tracks if exists remotely3. Updating Remote Branches
# Fetch all remote changes
git fetch origin
# Update remote tracking branches
git remote update origin
# Pull changes for current branch
git pull origin feature-loginRebasing vs Merging
1. Merge
Creates a merge commit, preserving the complete history.
git checkout main
git merge feature2. Rebase
Reapplies commits on top of the target branch, creating linear history.
git checkout feature
git rebase mainWhen to Use Each
Use Merge when:
- You want to preserve the exact history
- Working on shared branches
- Need to see when features were merged
Use Rebase when:
- You want clean, linear history
- Working on personal branches
- Before merging feature branches
Best Practices
1. Branch Naming Conventions
# Good naming
feature/user-authentication
bugfix/login-error
hotfix/security-patch
release/v1.2.0
# Avoid
temp
stuff
fix2. Keep Branches Updated
# Before starting work
git checkout main
git pull origin main
git checkout -b feature-new
# or update existing feature branch
git checkout feature-existing
git rebase main3. Regular Cleanup
# Delete merged branches
git branch --merged | grep -v "main\|develop" | xargs git branch -d
# Clean up remote branches
git remote prune origin4. Use Descriptive Commit Messages
# Good
git commit -m "feat: Add user authentication with JWT"
git commit -m "fix: Resolve login validation error"
git commit -m "docs: Update API documentation"
# Avoid
git commit -m "fixed stuff"
git commit -m "wip"
git commit -m "temp"Useful Git Commands
1. Branch Comparison
# Show differences between branches
git diff main..feature
# Show commits in feature but not in main
git log main..feature
# Show commits in main but not in feature
git log feature..main
# Show commits in both branches
git log --oneline --graph --decorate main feature2. Branch History
# Show branch history
git log --oneline --graph --all
# Show who created branches
git for-each-ref --format='%(authorname) %(refname:short)' --sort= -authordate
# Show last commit on each branch
git branch -v3. Branch Recovery
# Find deleted branch commit
git reflog
# Recover deleted branch
git checkout -b recovered-branch <commit-hash>Complete Workflow Example
#!/bin/bash
# Complete feature development workflow
# 1. Update main branch
echo "Updating main branch..."
git checkout main
git pull origin main
# 2. Create feature branch
echo "Creating feature branch..."
git checkout -b feature-new-dashboard
# 3. Do some work
echo "Making changes..."
echo "Dashboard component" > dashboard.js
git add dashboard.js
git commit -m "feat: Add dashboard component"
# 4. More work
echo "Adding styles..."
echo "Dashboard styles" > dashboard.css
git add dashboard.css
git commit -m "feat: Add dashboard styles"
# 5. Update with latest main changes
echo "Updating with main..."
git fetch origin
git rebase origin/main
# 6. Push to remote
echo "Pushing to remote..."
git push -u origin feature-new-dashboard
# 7. Create pull request (manual step)
echo "Create pull request on GitHub/GitLab"
# 8. After review and merge
echo "Cleaning up..."
git checkout main
git pull origin main
git branch -d feature-new-dashboardExternal Resources:
Related Tutorials: