Navigating Git: Merge, Rebase, Squash

Jul 31, 2024

Understanding Git Commands: Merge, Rebase, and Squash

Introduction

  • Common confusion about using git merge, git rebase, or squashing commits.
  • Importance of understanding these commands in Git workflows.
  • Focus: Key differences and appropriate usage.

Workflow Scenario

  • Creating a Feature Branch:
    • Started from the main branch.
    • New commits on feature branch: A, B, C.
    • Concurrent commits on main branch: D, E.
    • Visualize branches as growing tree branches.

Keeping Feature Branch Up to Date

Options: git merge vs git rebase

  • git merge:
    • Pulls in the latest changes from main.
    • Creates a new "merge commit".
    • Analogy: Tying branches together with a knot.
  • git rebase:
    • Changes base of feature branch to latest main commit.
    • Replays changes from the feature branch.
    • Provides a cleaner, linear commit history.

Merging Feature Branch Back to Main

Options:

  1. Git Merge:
    • Creates a merge commit tying histories together.
    • Can lead to a messy history with many knots.
  2. Git Rebase and Fast-Forward Merge:
    • Moves feature branch changes to the tip of main.
    • Allows for a fast-forward merge, resulting in a straightened history.

Squashing Commits

  • Definition:
    • Combines all feature branch commits into a single commit when merging into main.
  • Benefits:
    • Maintains a linear history in the main branch.
    • Creates a single merge commit.
  • Drawbacks:
    • Loses detailed history of individual feature commits in main branch.
  • Usage:
    • Commonly used in platforms like GitHub for tidying up commit history.

Summary of Key Points

  • git merge:
    • Complete picture of commit history and branch evolution.
  • git rebase:
    • Tidies up history by moving commits to the tip of the main branch.
  • Squashing Commits:
    • Consolidates commits for a clean, linear history, sacrificing fine details.

Conclusion

  • Choosing between strategies depends on team preferences for history clarity versus detailed commit records.
  • No one-size-fits-all; evaluate pros and cons based on team values.

Discussion Prompt

  • Have you used these strategies? Which do you prefer and why?