You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Once work on a branch is complete, you need to integrate those changes back into the main line of development. Git provides git merge to combine the histories of two branches.
The simplest type of merge is a fast-forward. This happens when the target branch (usually main) has not diverged from the branch you are merging in — there is a straight line of commits:
git switch main
git merge feature/login
Git simply moves the main pointer forward to the tip of feature/login. No merge commit is created.
When both branches have new commits since they diverged, Git performs a three-way merge. It takes the two branch tips and their common ancestor commit, combines the changes, and creates a new merge commit:
git switch main
git merge feature/dashboard
Git opens your editor for a merge commit message, then records the merge. The history now shows two lines converging.
A merge conflict occurs when the same lines of the same file were changed differently on both branches. Git cannot resolve this automatically and asks you to intervene:
git merge feature/header
# CONFLICT (content): Merge conflict in header.js
# Automatic merge failed; fix conflicts and then commit the result.
Inside the conflicted file you will see conflict markers:
<<<<<<< HEAD
<h1>Welcome to My Site</h1>
=======
<h1>Hello, World!</h1>
>>>>>>> feature/header
Edit the file to keep the version you want (removing the markers), then stage and commit:
git add header.js
git commit -m "Merge feature/header — use updated heading text"
git merge --no-commit --no-ff feature/header
This performs the merge but stops before creating the commit so you can inspect the result.
If you decide you do not want to complete a merge while conflicts are unresolved:
git merge --abort
This returns your repository to the state it was in before you ran git merge.
An alternative to merging is rebasing, which replays your branch's commits on top of the target branch to produce a linear history. Rebasing is more advanced and rewrites commit history — for this course, merging is the recommended approach.
# Branches already merged into main
git branch --merged
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.