Git Merge Explained: Examples, Fast-Forward & Conflict Resolution Guide

Git Merge Explained: Examples, Fast-Forward & Conflict Resolution Guide

Git merge is one of the most commonly used commands to combine changes from different branches created using git branch basics. Whether you're integrating feature work, resolving conflicts, or understanding fast-forward merges, knowing the right commands is essential. This guide covers all key Git merge scenarios with practical examples and a quick cheat sheet.


Git Merge Commands - Quick Cheat Sheet

TaskCommand
Merge branch into current branchgit merge <branch>
Merge branch into specific branchgit checkout main && git merge feature
Merge with fast-forward (default)git merge <branch>
Force no fast-forward mergegit merge --no-ff <branch>
Squash commits before mergegit merge --squash <branch>
Merge specific commitgit merge <commit-hash>
Merge multiple branchesgit merge branch1 branch2
Abort ongoing mergegit merge --abort
Continue merge after resolving conflictsgit merge --continue
Check merge statusgit status
View merge historygit log --oneline --graph
Show changes before merginggit diff <branch>
Fetch latest changes before mergegit fetch --all
Pull and merge in one stepgit pull
Resolve conflicts manuallyEdit file → git add .git commit
View conflicting filesgit status
Use merge strategy (recursive)git merge -s recursive <branch>
Use ours strategygit merge -s ours <branch>
Use theirs strategygit merge -X theirs <branch>
Prevent commit during mergegit merge --no-commit <branch>
Add custom merge messagegit merge -m "message" <branch>
Allow unrelated historiesgit merge --allow-unrelated-histories
Revert a merge commitgit revert -m 1 <merge-commit>
Check merged branchesgit branch --merged
Check unmerged branchesgit branch --no-merged
Delete merged branchgit branch -d <branch>
Force delete branchgit branch -D <branch>
Compare branches before mergegit diff branch1..branch2
Visualize branch historygit log --graph --all --oneline
Dry run merge (simulate)git merge --no-commit --no-ff <branch>

What is Git Merge?

Git merge is used to combine changes from one branch into another, typically integrating feature work into the main branch. It preserves the history of both branches and creates a unified commit history. This is a core operation in Git workflows, especially in collaborative development as part of a complete Git workflow.

What does git merge do

The git merge command takes changes from a specified branch and integrates them into your current branch.

text
git merge feature-branch

This command:

  • Combines commit history from both branches
  • Creates a new merge commit (in most cases)
  • Updates the current branch with merged changes

When to use git merge vs rebase

  • Use git merge when you want to preserve complete branch history
  • Use git rebase when you want a clean, linear history using git rebase

Example:

text
git merge feature-branch
git rebase feature-branch

Key difference:

  • Merge → keeps history intact
  • Rebase → rewrites history

How git merge works internally

Git identifies a common ancestor (base commit) between branches and applies changes accordingly, which you can inspect using git log.

There are two main ways Git performs a merge:

  • Fast-forward merge
  • Three-way merge

Git compares:

  • Base commit
  • Source branch changes
  • Target branch changes

Then it combines them into a single branch state.


Basic Git Merge Example

Merge feature branch into main

Switch to the target branch using git checkout:

text
git checkout main

Merge the feature branch:

text
git merge feature-branch

Step-by-step git merge workflow

text
git checkout main
git fetch --all
git pull
git merge feature-branch

This ensures your branch is updated using git fetch and ready for merging.

  • You are on the correct branch
  • Your branch is updated
  • Merge happens without conflicts (if possible)

Verify merge results

Check the status:

text
git status

View commit history:

text
git log --oneline --graph

If successful, you will see the merged commits reflected in your history.


Fast-Forward vs Three-Way Merge

What is fast-forward merge

A fast-forward merge happens when there are no new commits in the target branch.

text
git merge feature-branch

Git simply moves the pointer forward without creating a new commit.

When fast-forward merge happens

  • No divergence between branches
  • Feature branch is ahead of main
  • No additional commits in main after branching

Result:

  • Clean linear history
  • No merge commit

What is three-way merge

A three-way merge occurs when both branches have diverged.

Git uses:

  • Common ancestor
  • Source branch commit
  • Target branch commit

Example:

text
git merge feature-branch

Git creates a new merge commit combining both histories.

Differences between fast-forward and no-fast-forward

FeatureFast-Forward MergeNo-Fast-Forward Merge
Merge commitNot createdCreated
HistoryLinearPreserved with branch context
Commandgit merge branchgit merge --no-ff branch
Use caseSimple updatesTeam collaboration

To force no-fast-forward:

text
git merge --no-ff feature-branch

This ensures a merge commit is always created, preserving branch history.


Merge Branch with --no-ff

Using --no-ff ensures that Git always creates a merge commit, even if a fast-forward merge is possible. This helps preserve branch history and makes it easier to track feature development.

Why use --no-ff merge

By default, Git performs a fast-forward merge when possible. However, this can hide the context of feature branches.

Using:

text
git merge --no-ff feature-branch

ensures:

  • A merge commit is always created
  • Branch history remains visible
  • Easier tracking of feature work

Preserve branch history

Without --no-ff, Git may flatten history:

text
main → A → B → C

With --no-ff, history is preserved:

text
main → A → B → C
          \       \
           D → E → M (merge commit)

This is useful in team environments where understanding changes is important.

Example with real workflow

text
git checkout main
git pull
git merge --no-ff feature-login -m "Merge feature-login into main"

This creates a clear merge commit with a descriptive message.


Handle Merge Conflicts (Real-World Case)

Why conflicts occur during merge

Conflicts happen when:

  • Same file is modified in both branches
  • One branch deletes a file while another modifies it
  • Conflicts happen when changes overlap, which you can analyze using git diff.

Identify conflict using git status

Run:

text
git status

You will see:

text
both modified: file.txt

Git marks conflicting sections inside files using:

text
<<<<<<< HEAD
=======
>>>>>>> feature-branch

Resolve conflict step-by-step

  1. Open the conflicting file
  2. Manually resolve differences
  3. Remove conflict markers
  4. Stage the file using git add:
text
git add file.txt
  1. Complete the merge using git commit:
text
git commit

Useful commands to fix conflicts

text
git merge --abort      # Cancel merge
git diff               # View differences
git log --merge        # View conflicting commits
git checkout --ours    # Keep current branch changes
git checkout --theirs  # Accept incoming changes

Merge Specific Commits and Advanced Usage

Merge a specific commit

To merge a specific commit:

text
git merge <commit-hash>

Useful when you want only selected changes instead of full branch merge, similar to git cherry pick.

Merge multiple branches

You can merge multiple branches at once:

text
git merge branch1 branch2

Git will combine all specified branches into the current branch.

Squash merge vs normal merge

Squash merge combines all commits into a single commit:

text
git merge --squash feature-branch
git commit

Difference:

TypeBehavior
Normal mergePreserves full commit history
Squash mergeCombines commits into one

Use git merge strategies

Git supports different merge strategies:

text
git merge -s recursive feature-branch
git merge -s ours feature-branch
git merge -X theirs feature-branch

Common strategies:

  • recursive → default strategy
  • ours → keep current branch changes
  • theirs → prefer incoming branch changes

Git Merge vs Git Rebase

Key differences

FeatureGit MergeGit Rebase
HistoryPreservedRewritten
Commit typeMerge commitNo merge commit
Use caseCollaborationClean history

When to use merge vs rebase

  • Use merge for team collaboration and shared branches
  • Use rebase for cleaner commit history before pushing

Pros and cons

Git Merge

  • ✔ Preserves history
  • ✔ Safe for shared branches
  • ✖ Can create messy history

Git Rebase

  • ✔ Cleaner linear history
  • ✔ Easier to read logs
  • ✖ Rewrites history (risk in shared repos)

Frequently Asked Questions

1. What is git merge?

git merge is used to combine changes from one branch into another, typically integrating feature work into the main branch.

2. What does git merge do?

git merge combines commit histories from different branches and creates a unified branch, often generating a merge commit.

3. What is fast-forward merge in Git?

A fast-forward merge occurs when there are no new commits in the target branch, allowing Git to simply move the branch pointer forward.

4. How to use git merge command?

Use git merge to merge a branch into your current branch after switching to the target branch.

5. How to resolve merge conflicts in Git?

Edit conflicting files manually, remove conflict markers, then run git add and git commit to complete the merge.

6. What is the difference between git merge and git rebase?

git merge preserves commit history and creates a merge commit, while git rebase rewrites history to create a linear commit sequence.

7. How to abort a git merge?

Use git merge --abort to cancel an ongoing merge and revert to the previous state.

8. What is --no-ff in git merge?

The --no-ff option forces Git to create a merge commit even when a fast-forward merge is possible, preserving branch history.

9. How to squash commits during merge?

Use git merge --squash to combine all commits into a single commit before merging.

10. When should I use git merge?

Use git merge when working in collaborative environments where preserving complete branch history is important.

Summary

Git merge is essential for combining changes from different branches and managing collaborative workflows along with concepts explained in merge vs rebase. Whether you are performing a simple merge, handling conflicts, or using advanced strategies like squash or no-fast-forward, understanding these concepts helps maintain a clean and reliable project history.

Choosing between merge and rebase depends on your workflow, but mastering both ensures flexibility and better version control practices.


Official Documentation

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.