Understanding Merge Types

Advanced Git

Amanda Crawford-Adamo

Software and Data Engineer

I'm Amanda

A picture of Amanda

Advanced Git

What is git merge?

Git Merge Command:

git merge
  • Combines changes from one branch into another
  • Finds the common base between two branches
  • Use different merge strategies

Road where two roads join with two cars intersecting on the same road

Advanced Git

Fast-forward merge

What is fast forward merge?

  • Keep a simple, straight history
  • Ideal for short-lived branches with simple changes

$$

When not to use

  • Need to preserve branch history
  • Complex feature development in long-lived branches
  • Merge conflict between branches - fast forward fails immediately!
Advanced Git

Fast-forward merge (before)

A graph that shows to branches: main and feature branch. Main has two commits. Feature branch diverges after main latest commit with an additional commit in it's branch.

Advanced Git

Fast-forward merge syntax

Git Merge Fast Forward Default

git checkout main
git merge feature_branch

Force Git Merge Fast Forward

git merge <branch> --ff-only

Example

git checkout main
git merge feature_branch --ff-only
Advanced Git

Fast-forward merge (after)

A graph that shows to branches: main and feature branch. Main has three commits. Feature branch diverges after main second commit with an additional commit in it's branch. Main's third commit is a copy of feature branch's latest commit.

Advanced Git

Recursive merge

What is a recursive merge?

  • Creates a merge commit with two parents
  • Preserve the entire project history
  • Ideal for long-lived branches
  • Maintain branching structure

$$

When not to use

  • When you want to maintain a simple and linear history
  • Quick and minor changes
Advanced Git

Recursive merge (before)

A graph that shows to branches: main and feature branch. Main has three commits. Feature branch diverges after main first commit with an additional two commit in it's branch.

Advanced Git

Recursive merge syntax

Recursive Merge Command:

git merge --no-ff <branch>

Example

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ git merge --no-ff feature_branch
Merge made by the 'recursive' strategy.
...
Advanced Git

Recursive merge (after)

A graph that shows to branches: main and feature branch. Main has six commits. Feature branch diverges after main latest commit with an additional two commit in it's branch. Main branch is six commits are an interleave of all of main and feature commits combined. Main's latest commit is a merge commit object with two parents: the latest commit in the feature branch and main's previous latest commit.

Advanced Git

Summary

  • Fast forward merges keeps history simple and linear
  • Recursive merges preserves historical context
  • Recursive merges are better for more complex development

Fast Forward Merge Commands

git merge <branch_name> # default command
git merge --ff-only <branch_name> # force fast forward merge

Recursive Merge Command

git merge --no-ff <branch_name>
Advanced Git

Let's practice!

Advanced Git

Preparing Video For Download...