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.
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.
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.
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.
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
<<<<<<< HEADmarks the current branch's version.=======separates the two versions.>>>>>>> feature/employee-searchmarks 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.
git status to see which files require attention.git add for each resolved file.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.
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
| 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.
| 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.
9. Common Git Merge Mistakes
- Merging from the wrong branch: Always check
git branch --show-currentbefore merging. - Ignoring uncommitted changes: Check
git statusand 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.
- Create a feature branch from the latest agreed base branch.
- Implement the feature and commit meaningful changes.
- Push the branch to the remote repository.
- Create a GitHub pull request for review.
- Address review comments and resolve conflicts if required.
- Run automated checks and required tests.
- Merge the pull request using the team's chosen merge strategy.
- 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
mainwith suitable review and status-check rules. - Do not rewrite shared history without team coordination.
12. Interview Questions and Answers
13. Hands-On Practice
Try the following exercise to practise merging branches in your local repository.
feature/department-management from main.Department.cs and add a basic department model.Add department model.main and merge the feature branch.git log --oneline --graph --decorate --all.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.
git merge branch-name. Always review and test the result before sharing it with the team.15. Official Documentation and Next Article
- Git Documentation – git merge
- Pro Git – Basic Branching and Merging
- GitHub Documentation – Pull Requests
Next article: Git Remote Operations