Skip to main content

📝 Lesson 2: Branching and Merging

Creating parallel universes in your code.

🎯 Learning Objectives

By the end of this lesson, you will be able to:

  • Understand Git branching concepts.
  • Create, switch, and merge branches.
  • Resolve common merge conflicts.
In This Lesson

Understanding Branches

Branches allow you to work on new features or bug fixes in isolation from the main codebase. They act as independent timelines.

Use git switch (modern) or git checkout to navigate between them, and git branch to create new ones.

gitGraph commit id: "Initial commit" branch feature-x checkout feature-x commit checkout main merge feature-x

Merge Strategies

When you merge, Git decides how to combine the branches. Fast-forward merges move the branch pointer, while three-way merges create a new merge commit.

🔑 Key Insight: Merge vs. Rebase

Merging preserves history (you can see exactly when a feature was integrated). Rebasing rewrites history (it keeps the project timeline linear), making it cleaner but potentially dangerous if the branch is shared.

Merge Conflicts

Conflicts occur when Git cannot automatically reconcile differences. You'll need to manually edit the conflicted files to resolve them before finishing the merge.

Best Practices

  • Keep branches short-lived.
  • Use descriptive branch names (e.g., feature/login-form).
  • Regularly pull latest changes from the main branch.

💡 Pro Tip: Branch Naming

Adopt a convention for your branches to keep things organized. Common formats include feature/your-feature, bugfix/issue-description, or hotfix/critical-patch.