Beginner30 minutesGitLevel 2

Merging in Git

Learning Objectives

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

  • Understand Understand what merging is and why it is important
  • Merge branches in Git
  • Resolve merge conflicts
  • Follow best practices for merging

Why This Matters:

Merging is how collaborative development works. Every pull request, every feature - they all end with a merge. Understanding this process is essential for working on any team.

Before You Start:

You should be familiar with:

  • Be familiar with Git branching
  • Have a Git repository with multiple branches to practice merging

What is Merging?

Merging in Git is the process of combining the changes from one branch into another. This is a core part of collaborative workflows and feature development.

The most common merge is bringing changes from a feature branch into the main branch after the feature is complete.

How to Merge Branches

  1. Switch to the branch you want to merge into (usually main):
  2. Run the merge command:

    This will bring the changes from feature-branch into main.

Merge Conflicts

Sometimes, Git can't automatically combine changes and will report a merge conflict. This happens when the same part of a file was changed in both branches.

  1. Git will mark the conflict in your files:
  2. Edit the file to resolve the conflict, then stage and commit:

Tip: Use git status to see which files have conflicts and need to be resolved.

Best Practices for Merging

  • Pull the latest changes from main before merging your branch
  • Resolve conflicts as soon as they arise
  • Test your code after merging
  • Write clear commit messages when resolving conflicts

🏁 Lesson Complete: Git Merging

Key Takeaways:

  • Merging combines changes from one branch into another
  • Always switch to the target branch (e.g., main) before running git merge
  • Merge conflicts occur when the same lines are changed in both branches
  • Resolve conflicts by editing the file, then staging and committing
  • Best practices: pull latest changes before merging, test after merging, write clear commit messages

Learning Objectives Review:

Look back at what you set out to learn. Can you now:

  • Understand what merging is and why it is important Check!
  • Merge branches in Git Got it!
  • Resolve merge conflicts Can explain it!
  • Follow best practices for merging Could teach this!

If you can confidently answer "yes" to most of these, you're ready to move on!

Think & Reflect:

💭 Reflection Questions

  • What strategies can you use to minimize merge conflicts?
  • Why should you test your code after a merge?
  • How does merging relate to the pull request workflow?

Looking Ahead:

Now that you can merge branches and resolve conflicts, it's time to learn about remote repositories - how to share your code with the world using GitHub or GitLab.

Recommended Next Steps

Continue Learning

Ready to move forward? Continue with the next tutorial in this series:

Remote Repositories

Related Topics

Explore these related tutorials to expand your knowledge:

Practice Projects

Apply what you've learned with these hands-on projects:

Merge Conflict Practice

Practice resolving merge conflicts in a sample repo.

gitmergeconflict
Start Project

Additional Resources

Deepen your understanding with these helpful resources:

Progress tracking is disabled. Enable it in to track your completed tutorials.