Back to blog
March 29, 2024
4 min read

Git Merge Explained Simply

Learn how to combine branch changes effectively with Git merge

Git Merge Explained Simply

Merging is a fundamental Git operation that combines changes from different branches. This tutorial will guide you through the process of merging branches effectively and understanding different merge strategies.

What You’ll Learn

  • Perform basic merges
  • Understand merge strategies
  • Handle merge conflicts
  • Use merge options
  • Follow merge best practices

Implementation Steps

  1. Basic Merge

    # Switch to target branch
    git checkout main
    
    # Merge feature branch
    git merge feature-branch
    

    This combines changes from the feature branch into the main branch.

  2. Merge with Options

    # Merge with no fast-forward
    git merge --no-ff feature-branch
    
    # Merge with commit message
    git merge -m "Merge feature branch" feature-branch
    

    These options provide more control over the merge process.

  3. Abort Merge

    # Abort a merge in progress
    git merge --abort
    

    This cancels the current merge operation.

  4. Check Merge Status

    # Check merge status
    git status
    
    # View merge conflicts
    git diff
    

    This helps you understand the current merge state.

Merge Strategies

  1. Fast-Forward Merge

    Before:
    A---B---C (main)
         \
          D---E (feature)
    
    After:
    A---B---C---D---E (main)
    
  2. Three-Way Merge

    Before:
    A---B---C (main)
         \
          D---E (feature)
    
    After:
    A---B---C---F (main)
         \     /
          D---E
    

Best Practices

  1. Before Merging

    • Update your working directory
    • Check branch status
    • Review changes
    • Backup if needed
    • Consider merge strategy
  2. During Merge

    • Follow merge workflow
    • Resolve conflicts carefully
    • Test after merging
    • Commit merge changes
    • Document merge decisions
  3. After Merging

    • Verify merge success
    • Test functionality
    • Clean up branches
    • Update documentation
    • Push changes
  4. Merge Strategy Selection

    • Use fast-forward when possible
    • Consider three-way merge
    • Choose appropriate strategy
    • Follow team conventions
    • Document strategy choice

Common Use Cases

  1. Feature Branch Merge

    # Update main branch
    git checkout main
    git pull
    
    # Merge feature
    git merge feature-branch
    
    # Push changes
    git push
    
  2. Hotfix Merge

    # Create hotfix branch
    git checkout -b hotfix/issue-123
    
    # Fix issue and commit
    git commit -m "fix: resolve issue #123"
    
    # Merge to main
    git checkout main
    git merge hotfix/issue-123
    
    # Merge to develop
    git checkout develop
    git merge hotfix/issue-123
    
  3. Release Branch Merge

    # Create release branch
    git checkout -b release/v1.0.0
    
    # Prepare release
    git commit -m "chore: prepare release v1.0.0"
    
    # Merge to main
    git checkout main
    git merge release/v1.0.0
    
    # Merge to develop
    git checkout develop
    git merge release/v1.0.0
    

Advanced Usage

  1. Merge with Rebase

    # Rebase feature branch
    git checkout feature-branch
    git rebase main
    
    # Merge to main
    git checkout main
    git merge feature-branch
    
  2. Squash Merge

    # Squash merge feature branch
    git merge --squash feature-branch
    
    # Commit squashed changes
    git commit -m "feat: implement feature X"
    
  3. Merge Specific Files

    # Checkout specific file
    git checkout feature-branch -- path/to/file
    
    # Commit the file
    git commit -m "feat: add file from feature branch"
    

Common Issues and Solutions

  1. Merge Conflicts

    # See conflicts
    git status
    
    # Resolve conflicts
    # Edit conflicted files
    git add resolved-files
    git commit -m "Merge: resolve conflicts"
    
  2. Failed Merge

    # Abort merge
    git merge --abort
    
    # Try different strategy
    git merge -X theirs feature-branch
    
  3. Complex Merges

    # Use merge tool
    git mergetool
    
    # Or resolve manually
    # Edit files
    git add .
    git commit -m "Merge: resolve complex conflicts"
    

Conclusion

Understanding Git merge is crucial for effective collaboration. Remember to:

  • Choose appropriate strategy
  • Handle conflicts properly
  • Test after merging
  • Follow best practices
  • Document merge decisions

This knowledge will help you maintain a clean and efficient Git workflow.

Next Steps

After mastering Git merge, you might want to:

  • Learn about Git rebase
  • Understand merge strategies
  • Master conflict resolution
  • Learn about Git hooks
  • Explore Git workflows

Remember that proper merging is key to successful collaboration.