LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Git & GitHub

Git Merge – Combine Changes from Multiple Branches

When developers work on separate features, they usually create separate branches. After completing the work, they need to bring those changes back into the main branch. Git merge helps combine that work.

Git Merge – Combine Changes from Multiple Branches

Imagine your team is developing an employee management application. One developer is working on employee search, another is building department management, and a third is fixing a login issue. Each developer works on a separate Git branch so that unfinished changes do not disturb the main application.

Once a feature is ready, its changes need to become part of the main development line. This is where Git Merge becomes important.

In this article: Learn how Git merge combines changes from different branches, understand fast-forward and three-way merges, perform a practical feature-branch merge, resolve merge conflicts, and use safe team merging practices.

1. What Is Git Merge?

Git merge is a command used to combine changes from one branch into another branch. It allows developers to integrate independently developed features, bug fixes, and other changes into a shared codebase.

For example, suppose you have two branches:

  • main – contains the stable application code.
  • feature/employee-search – contains the employee search implementation.

When the feature is ready, you can merge it into main. Git combines the changes and records the result in the target branch.

2. How Git Merge Works

A merge is performed from the branch that should receive the changes. In other words, first switch to the destination branch, then merge the source branch into it.

The main branch and feature branch contain separate commits. The feature branch is merged into main, creating a merge commit that connects both histories. main feature/employee-search A B C M A D E Initial Commit Commit Merge commit Feature changes Changes integrated Both branch histories are connected in the target branch

Conceptual illustration: the feature branch's commits are integrated into main. The exact commit graph depends on the merge strategy and branch history.

Git examines the commit histories of the branches and determines how to combine them. Depending on their relationship, Git may perform a fast-forward merge or create a merge commit using a three-way merge.

3. Understanding the Two Main Merge Types

3.1 Fast-Forward Merge

A fast-forward merge is possible when the target branch has not received new commits since the feature branch was created. The target branch can simply move forward to the latest commit of the feature branch.

Example: main points to commit A, and the feature branch contains commits B and C after A. Git can move the main branch pointer to C without creating an additional merge commit.

Fast-forward merge example

# Switch to the destination branch
git switch main

# Merge the feature branch
git merge feature/employee-search

Typical terminal output

Updating 4a2b8c1..8d91f20
Fast-forward
 EmployeeSearch.cs | 45 +++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

3.2 Three-Way Merge

A three-way merge is used when both branches have moved forward independently. Git identifies their common ancestor and compares the changes made on both branches.

For example, while the feature branch adds employee search, another developer may add logging to main. Git combines the changes when they do not conflict. A merge commit is generally created to connect the two histories.

Two branches diverge from a common ancestor. One branch adds a logging commit, the other adds a search feature commit, and both join at a merge commit. main feature A B C D E F M Common ancestor Main change Feature change Merge

A three-way merge uses the common ancestor and the two branch tips to determine the combined result.

4. Practical Git Merge Example

Let's use an employee management application. A developer will create an employee search feature in a separate branch and merge it into main.

Step 1: Check the current branch and working tree

Before merging, inspect the repository status and available branches.

git status
git branch
On branch main
nothing to commit, working tree clean

  feature/employee-search
* main

Step 2: Switch to the feature branch

git switch feature/employee-search
Switched to branch 'feature/employee-search'

Step 3: Implement and commit the feature

After implementing employee search, stage and commit the changes.

git add EmployeeSearch.cs
git commit -m "Add employee search feature"
[feature/employee-search 8d91f20] Add employee search feature
 1 file changed, 45 insertions(+)

Step 4: Switch back to main

The destination branch is main, so switch to it before running the merge command.

git switch main

Step 5: Merge the feature branch

git merge feature/employee-search
Updating 4a2b8c1..8d91f20
Fast-forward
 EmployeeSearch.cs | 45 +++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Step 6: Verify the merge

git status
git log --oneline --graph --decorate -5

Review the commit history and confirm that the expected changes are now part of main.

Important: A successful merge does not automatically mean the application is correct. Run the relevant tests, build the application, and review the changes before considering the integration complete.

5. What Is a Merge Conflict?

A merge conflict occurs when Git cannot automatically combine changes. A common example is when two branches modify the same lines of the same file in incompatible ways.

Consider a file named EmployeeService.cs. The main branch changes the employee validation message, while the feature branch changes the same line to a different message. Git cannot decide which version represents the intended result.

How Git marks a conflict

<<<<<<< HEAD
return "Employee details are required";
=======
return "Please enter valid employee details";
>>>>>>> feature/employee-search
  • <<<<<<< HEAD marks the current branch's version.
  • ======= separates the two versions.
  • >>>>>>> feature/employee-search marks the incoming branch's version.

These markers are temporary conflict indicators. They are not valid C# code and must be removed when resolving the conflict.

6. How to Resolve a Merge Conflict

When a conflict occurs, Git pauses the merge and asks you to resolve the affected files.

Step 1 – Identify conflicted filesRun git status to see which files require attention.
Step 2 – Open the affected filesReview both versions and understand why the changes were made. Do not blindly select one side.
Step 3 – Resolve the conflictEdit the file to produce the correct combined implementation. Remove all conflict markers.
Step 4 – Stage the resolved filesUse git add for each resolved file.
Step 5 – Complete the mergeCommit the merge if Git has not already completed it automatically.

Conflict resolution commands

git status

# After editing and resolving the conflicted file
git add EmployeeService.cs

# Complete the merge
git merge --continue

Alternative: create the merge commit

git commit

Git normally prepares a merge commit message for you. Save and close the configured editor to complete the commit.

Do not commit unresolved conflict markers. Review the final code, run tests, and confirm that both branches' intended changes have been preserved.

Cancel a merge in progress

If you want to abandon the current merge and return to the pre-merge state, use:

git merge --abort

This is useful when you decide that the merge should not continue. Be careful with uncommitted work: understand the repository state before aborting.

7. Useful Git Merge Options

Common Git merge commands and their purpose
Command Purpose
git merge branch-name Merge the specified branch into the current branch.
git merge --no-ff branch-name Create a merge commit even when a fast-forward merge is possible.
git merge --ff-only branch-name Allow only a fast-forward merge; fail if one is not possible.
git merge --abort Abort a merge that is currently in progress.
git merge --continue Continue a merge after conflicts have been resolved and staged.
git merge --squash branch-name Prepare the combined changes as a single staged change without creating a merge commit.

Understanding --no-ff

By default, Git may use fast-forward when possible. The --no-ff option forces a merge commit, preserving a visible point in history where the feature branch was integrated.

git switch main
git merge --no-ff feature/employee-search

Understanding --squash

A squash merge combines the incoming branch's changes into the working tree and index as one staged change. It does not create a merge commit or record the source branch's individual commits as part of the target branch's ancestry. You create the resulting commit yourself.

git switch main
git merge --squash feature/employee-search
git commit -m "Add employee search feature"

8. Git Merge vs Git Rebase

Both merge and rebase integrate changes from another line of development, but they produce different commit histories.

Comparison of Git merge and Git rebase
Aspect Git Merge Git Rebase
Purpose Combines branch histories. Replays commits on top of another base.
History Preserves the original branch ancestry. Creates new commit IDs for replayed commits.
Merge commit May create a merge commit. Does not create a merge commit by itself.
Conflict handling Resolve conflicts during the merge. Resolve conflicts as commits are replayed.
Shared branches Generally suitable for integrating shared history. Avoid rebasing commits that others may already depend on.

Use merge when you want to preserve the actual integration history. Rebase is useful for creating a linear history, especially when updating a private feature branch before sharing it.

Team rule: Avoid rewriting published shared history unless your team has explicitly agreed on the workflow. Rebasing shared commits can create confusion for other developers.

9. Common Git Merge Mistakes

  • Merging from the wrong branch: Always check git branch --show-current before merging.
  • Ignoring uncommitted changes: Check git status and commit, stash, or otherwise safely handle your work before merging.
  • Choosing a conflict version without understanding it: Review the business logic and combine changes correctly.
  • Forgetting to test after merging: Build and test the integrated code.
  • Deleting a branch before verifying integration: Confirm the changes are safely integrated and pushed before deleting the branch.
  • Using force operations unnecessarily: Avoid destructive commands when a normal merge can solve the problem.

10. Real-World Team Workflow

In a professional development team, developers usually work on feature branches and integrate their work through pull requests.

  1. Create a feature branch from the latest agreed base branch.
  2. Implement the feature and commit meaningful changes.
  3. Push the branch to the remote repository.
  4. Create a GitHub pull request for review.
  5. Address review comments and resolve conflicts if required.
  6. Run automated checks and required tests.
  7. Merge the pull request using the team's chosen merge strategy.
  8. Delete the feature branch when it is no longer required.

Git merge is the underlying Git operation that brings branch changes together. A GitHub pull request is a collaboration and review workflow that helps a team discuss and approve those changes before integration.

11. Best Practices

  • Keep feature branches focused on one task.
  • Integrate changes regularly to reduce large conflicts.
  • Use descriptive commit messages.
  • Review the changes before merging.
  • Run automated tests and builds after integration.
  • Agree on merge, squash, or rebase conventions within the team.
  • Protect important branches such as main with suitable review and status-check rules.
  • Do not rewrite shared history without team coordination.

12. Interview Questions and Answers

1. What is Git merge? Git merge combines changes from one branch into the currently checked-out branch. It may perform a fast-forward merge or create a merge commit depending on the branch histories.
2. What is the difference between fast-forward and three-way merge? A fast-forward merge moves the target branch pointer forward when it has no independent commits. A three-way merge uses the common ancestor and both branch tips to combine independently developed changes.
3. What causes a merge conflict? A conflict occurs when Git cannot automatically combine changes, such as incompatible edits to the same lines in a file. A developer must review and resolve the changes.
4. What is the purpose of git merge --no-ff? It forces Git to create a merge commit even when a fast-forward merge is possible. This can make feature integration points visible in the commit history.
5. What is the difference between git merge and git rebase? Merge combines branch histories and can preserve a merge commit. Rebase replays commits onto a new base, creating new commit IDs and generally producing a linear history.

13. Hands-On Practice

Try the following exercise to practise merging branches in your local repository.

Exercise: Build and merge a department featureCreate a branch named feature/department-management from main.
Task 1Create a file named Department.cs and add a basic department model.
Task 2Commit the new file with the message Add department model.
Task 3Switch to main and merge the feature branch.
Task 4Inspect the result using git log --oneline --graph --decorate --all.
Task 5Repeat the exercise by making separate changes to the same line on both branches. Observe how Git reports a merge conflict and practise resolving it.

14. Conclusion

Git merge is an essential part of collaborative software development. It allows developers to work independently on separate branches and bring their changes together safely.

Understanding fast-forward merges, three-way merges, merge conflicts, and merge options helps you maintain a clear and reliable project history. In real-world teams, combine these skills with code reviews, automated tests, and a consistent branching strategy.

Key takeaway: Switch to the branch that should receive the changes, inspect the repository state, and run git merge branch-name. Always review and test the result before sharing it with the team.

15. Official Documentation and Next Article

Next article: Git Remote Operations

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.