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.
$ 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.
$ 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
$ 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.
$ 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
$ git branch –merged
feature-cart
feature-faq
feature-search
* main
Merge types
| Type | When | Result |
|---|---|---|
| Fast-forward | Main did not move | No merge commit, linear history |
| Three-way | Both branches have new commits | A merge commit with two parents |
Squash (--squash) | You want one commit for the full branch | Changes are staged, you make the commit |
| Conflict | Same lines changed in both | You must solve it by hand |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |