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.

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.
-
Verify your current position: First, ensure you are on the branch you want to merge into. In most workflows, this is
main.Bashgit switch main -
Perform the merge: Now, bring the changes from
login-pageintomain:Bashgit merge login-page -
What happens under the hood: If your
mainbranch hasn't changed since you created thelogin-pagebranch, Git performs a "fast-forward" merge. It simply moves themainpointer forward to the latest commit oflogin-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:
- Create a new branch named
feature-notes:git branch feature-notes. - Switch to that branch:
git switch feature-notes. - Create a file named
notes.txt, add some text, and commit it:Bashecho "My project notes" > notes.txt git add notes.txt git commit -m "Add project notes" - Switch back to
main:git switch main. - Merge your feature:
git merge feature-notes. - Run
git log --onelineto see the new commit history.
Common Pitfalls
- Merging into the wrong branch: Always check
git statusorgit branchbefore running a merge. You don't want to accidentally pull experimental code into your stablemainbranch. - 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.
Work with me

AI Chatbot & LLM Integration for Your App or Website
Add a smart AI chatbot or LLM feature to your product — trained on your content, integrated into your stack, and shipped by an AI-native engineer.

AI Automation & Agentic Workflow Development
Automate the repetitive work eating your time — content pipelines, data workflows, and agentic AI tasks that run themselves.


