Git Merge

The git merge command joins the work of one branch into another. You first move to the branch which will receive the changes (usually main), and then merge the other branch into it.

Syntax

git switch main
git merge <branch>

Fast-forward merge

If main has no new commits since the branch was created, Git only moves the main pointer forward. No new commit is needed.

Terminal output

$ git switch -c feature-cart
Switched to a new branch 'feature-cart'
$ echo "cart" > cart.html
$ git add .
$ git commit -q -m "Add cart page"
$ git switch main
Switched to branch 'main'
$ git merge feature-cart
Updating 52a1595..c8fd320
Fast-forward
cart.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 cart.html
$ git log –oneline
c8fd320 (HEAD -> main, feature-cart) Add cart page
52a1595 Add home page

The line Fast-forward tells what happened. The history is a straight line.

Three-way merge (merge commit)

If both branches have new commits, the history is split. Git creates a special merge commit with two parents to join them.

Terminal output

$ git switch -c feature-search
Switched to a new branch 'feature-search'
$ echo "search" > search.html
$ git add .
$ git commit -q -m "Add search page"
$ git switch main
Switched to branch 'main'
$ echo "about" > about.html
$ git add .
$ git commit -q -m "Add about page"
$ git merge feature-search -m "Merge feature-search"
Merge made by the 'ort' strategy.
search.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 search.html

Terminal output

$ git log –oneline –graph
* 59a3ac4 (HEAD -> main) Merge feature-search
|\
| * ae5af26 (feature-search) Add search page
* | b56e16a Add about page
|/
* c8fd320 (feature-cart) Add cart page
* 52a1595 Add home page

The graph shows the two lines coming together in the merge commit at the top.

Always create a merge commit (–no-ff)

The option --no-ff (no fast-forward) makes a merge commit even when a fast-forward is possible. Many teams use it to keep the history of the feature branch visible.

Terminal output

$ git switch -c feature-faq
Switched to a new branch 'feature-faq'
$ echo "faq" > faq.html
$ git add .
$ git commit -q -m "Add faq page"
$ git switch main
Switched to branch 'main'
$ git merge –no-ff feature-faq -m "Merge feature-faq"
Merge made by the 'ort' strategy.
faq.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 faq.html
$ git log –oneline –graph
* 377bbff (HEAD -> main) Merge feature-faq
|\
| * 491faff (feature-faq) Add faq page
|/
* 59a3ac4 Merge feature-search
|\
| * ae5af26 (feature-search) Add search page
* | b56e16a Add about page
|/
* c8fd320 (feature-cart) Add cart page
* 52a1595 Add home page

Cancel a merge

If the merge stops with conflicts and you want to go back to the state before the merge:

git merge --abort

See which branches are merged

Terminal output

$ git branch –merged
feature-cart
feature-faq
feature-search
* main

Merge types

TypeWhenResult
Fast-forwardMain did not moveNo merge commit, linear history
Three-wayBoth branches have new commitsA merge commit with two parents
Squash (--squash)You want one commit for the full branchChanges are staged, you make the commit
ConflictSame lines changed in bothYou must solve it by hand
Note: After a merge you can delete the feature branch with git branch -d. If the same lines are changed on both sides, read the next chapter: Git Merge Conflict.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Create a new branch and switch to it
Merge a feature branch into main
Resolve a merge conflict
Delete a merged branch
Rename a local branch
Copy one commit to another branch with cherry-pick
Interview Questions & Answers
How you can see list of all branches in Git?
How do I create a new branch in Git?
How to switch to an another branch in Git?
How to merge one branch to another in Git?
How to rename a local Git branch?
What is the difference between git merge and git rebase?
What is a merge conflict and how do you resolve it?