The Wayback Machine - https://web.archive.org/web/20260405044856/https://www.geeksforgeeks.org/git/git-merge/

Git Merge

Last Updated : 17 Mar, 2026

Git Merge combines changes from different branches into a single branch, integrating work while preserving history. It helps unify development without losing progress.

  • Preserves commit history and creates a merge commit when needed.
  • Performs fast-forward or automatic merges unless conflicts occur.
  • Commonly used to integrate feature branches into the main branch.

Working

Using the diagrams below, we will see how git merge works what the repository looks like before the merge and how Git creates a new merge commit to combine histories.

Before Merge

gitmerge

This image shows the state of the repository before the merge takes place.

Elements

  • Common Base: The commit from which both the main and feature (dev) branches originated.
  • Main Branch (in blue): This is your production or main branch where stable code lives.
  • Feature/Dev Branch (in green): This branch is where new development work has occurred.
  • Main Tip: The latest commit on the main branch.
  • Feature Tip: The latest commit on the feature branch.

Interpretation

  • Development has happened in parallel on both the main and feature branches.
  • These branches diverged from a common ancestor (the Common Base).
  • You are now preparing to merge the feature branch back into the main branch.

After Merge

git after merge
Git - Merge

This image shows what happens after executing a git merge.

During the merge, Git compares the latest commit of the main branch (Main Tip), the feature branch (Feature Tip), and their shared ancestor (Common Base). If there are no conflicts, Git automatically creates a new merge commit combining both histories.

Types of Merging in Git

Git supports several types of merging. The two most common types are:

1. Fast-forward merging

Occurs when Git advances the current branch pointer to the latest commit without creating a merge commit.

  • Happens when the target branch is directly ahead of the current branch.
  • Git moves the branch pointer forward instead of creating a new commit.
  • Fast-forward merging only possible when branches have not diverged.
git forwarded merging.
Git - Merge

2. Three-way merging

Occurs when branches have diverged, so Git creates a new merge commit by combining their changes.

  • Happens when the base branch has new commits after branching.
  • Git compares both branches with a common base commit.
  • A new merge commit is created to combine changes.
git three-way merging
Git - Merge

Note: Git also supports recursive and octopus merging, where recursive is the default strategy for complex histories and octopus merge combines multiple branches in a single commit.

Steps to Perform Git Merge

To ensure smooth merging, follow these steps:

Step 1: Switch to the Target Branch

git checkout <target-branch>

Step 2: Pull the Latest Changes

git pull origin <target-branch>

Step 3: Merge the feature Branch

git merge <feature-branch>

If conflicts occur, resolve them manually and then commit.

git-merge-dev

Step 4: Test the Merged Code

Verify that the application works correctly after merging.

Step 5: Push the change

git push origin <target-branch>

Git Merge Vs Rebase

Git merge combines branch histories with a merge commit, while rebase rewrites commits to create a linear history.

git_merge_vs_rebase
Git Merge vs Git Rebase
Git MergeGit Rebase
Combines changes from one branch into another with a merge commit.Applies commits from one branch onto another by rewriting history.
Preserves the complete commit history.Creates a linear history by removing merge commits.
Useful for integrating feature branches.Ideal for a clean, simplified project history.
Does not alter existing commits.Rewrites commit hashes and order.
Comment

Explore