Back to Blog
Lesson 17 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 4, 20263 min read

Merging Branches: How to Use git merge for Integration

Learn how to use git merge to combine changes from feature branches into your main project, a critical skill for seamless software integration.

gitversion controlbranchinggit mergeintegration
Close-up of software development tools displaying code and version control systems on a computer monitor.

Previously in this course, we covered Creating Branches: How to Use Git Branch to Manage Workflows and Switching Branches: How to Use git switch for Context Switching. Now that you know how to isolate your work in separate branches, this lesson adds the final, most important step: bringing those changes back into your primary project history.

The Mechanics of Git Merge

In Git, git merge is the primary tool for integration. When you merge one branch into another, you are essentially telling Git to find the common ancestor of both branches and combine the divergent histories into a new "merge commit."

Think of your project history as a tree. When you create a branch, you create a new limb. When you perform a merge, you are grafting that limb back onto the main trunk. This is how you consolidate work, share features with teammates, and keep your production code up to date.

Worked Example: Integrating a Feature

Let's assume you have a project with a main branch, and you've been working on a new feature in a branch called login-page.

  1. Verify your current position: First, ensure you are on the branch you want to merge into. In most workflows, this is main.

    Bash
    git switch main
  2. Perform the merge: Now, bring the changes from login-page into main:

    Bash
    git merge login-page
  3. What happens under the hood: If your main branch hasn't changed since you created the login-page branch, Git performs a "fast-forward" merge. It simply moves the main pointer forward to the latest commit of login-page. If both branches have evolved, Git creates a new commit that records the union of the two histories.

Hands-on Exercise

Follow these steps to practice integration in your local repository:

  1. Create a new branch named feature-notes: git branch feature-notes.
  2. Switch to that branch: git switch feature-notes.
  3. Create a file named notes.txt, add some text, and commit it:
    Bash
    echo "My project notes" > notes.txt
    git add notes.txt
    git commit -m "Add project notes"
  4. Switch back to main: git switch main.
  5. Merge your feature: git merge feature-notes.
  6. Run git log --oneline to see the new commit history.

Common Pitfalls

  • Merging into the wrong branch: Always check git status or git branch before running a merge. You don't want to accidentally pull experimental code into your stable main branch.
  • Assuming it's always clean: If both branches have modified the same lines in the same file, Git won't know which version to keep. This results in a "merge conflict," which we will cover in the next lesson.
  • Forgetting to save work: Git will stop you from switching branches if you have uncommitted changes that would be overwritten. Always commit or stash your work before starting an integration.

FAQ

What is the difference between a merge and a pull? A git pull is actually a combination of two commands: git fetch (downloading remote history) followed immediately by git merge. We will dive into this when we discuss remote repositories.

Does git merge delete the feature branch? No. After a successful merge, the feature branch still exists. It is common practice to delete it once the code is safely integrated, but that is a separate manual step we will explore soon.

Can I merge main into my feature branch? Yes, and you should! If main moves forward while you are working on a feature, merging main into your feature branch keeps your work updated with the latest project developments.

Recap

Integration is the heartbeat of collaborative development. By using git merge, you transform isolated experiments into part of your project's permanent record. You now know how to navigate between branches and combine them safely.

Up next: Handling Merge Conflicts — identifying and resolving collisions when Git can't decide which changes to keep.

Similar Posts